zoukankan      html  css  js  c++  java
  • Find them, Catch them

    Find them, Catch them
    Time Limit: 1000MS Memory Limit: 10000K
    Total Submissions: 36488 Accepted: 11188

    Description
    The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to. The present question is, given two criminals; do they belong to a same clan? You must give your judgment based on incomplete information. (Since the gangsters are always acting secretly.)

    Assume N (N <= 10^5) criminals are currently in Tadu City, numbered from 1 to N. And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. You will be given M (M <= 10^5) messages in sequence, which are in the following two kinds:

    1. D [a] [b]
      where [a] and [b] are the numbers of two criminals, and they belong to different gangs.

    2. A [a] [b]
      where [a] and [b] are the numbers of two criminals. This requires you to decide whether a and b belong to a same gang.

    Input
    The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. Each test case begins with a line with two integers N and M, followed by M lines each containing one message as described above.

    Output
    For each message “A [a] [b]” in each case, your program should give the judgment based on the information got before. The answers might be one of “In the same gang.”, “In different gangs.” and “Not sure yet.”

    Sample Input

    1
    5 5
    A 1 2
    D 1 2
    A 1 2
    D 2 4
    A 1 4

    Sample Output

    Not sure yet.
    In different gangs.
    In the same gang.

    Source
    POJ Monthly–2004.07.18
    题意:有两个犯罪团伙,D 操作就是表明两个人不是同一伙,A 查询两个人的关系

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #define LL long long
    using namespace std;
    
    const int MAX = 110000;
    
    struct node
    {
        int pre;
        int rea;
    }a[MAX];
    
    int n,m;
    
    void init()
    {
        for(int i=0;i<=n;i++)
        {
            a[i].pre=i;
            a[i].rea=0;
        }
    }
    
    int Find(int x)
    {
        if(a[x].pre==x)
        {
            return  x;
        }
        int tmp=a[x].pre;
        a[x].pre=Find(tmp);
        a[x].rea=(a[tmp].rea+a[x].rea)%2;
        return a[x].pre;
    }
    
    void Join(int x,int y,int b,int c)
    {
        if(b!=c)
        {
            a[b].pre=c;
            a[b].rea=(1+a[x].rea+2-a[y].rea)%2;
        }
    }
    int main()
    {
        int T;
        char s[10];
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d %d",&n,&m);
            init();
            int u,v;
            for(int i=0;i<m;i++)
            {
                scanf("%s %d %d",s,&u,&v);
                int b=Find(u);
                int c=Find(v);
                if(s[0]=='D')
                {
                    Join(u,v,b,c);
                }
                else
                {
                    if(b!=c)
                    {
                        printf("Not sure yet.
    ");
                    }
                    else
                    {
                        if(a[u].rea==a[v].rea)
                        {
                            printf("In the same gang.
    ");
                        }
                        else
                        {
                            printf("In different gangs.
    ");
                        }
                    }
                }
            }
    
        }
        return 0;
    }
    
  • 相关阅读:
    UML中类图的符号解释
    Vim简明教程【CoolShell】
    C++ 指针—02 指针与引用的对照
    一个通用onReady函数的实现
    内存泄漏以及常见的解决方法
    个人博客之路
    WPF 设置WebBrowser控件不弹脚本错误提示框
    Solr使用入门指南
    用C语言写解释器(一)——我们的目标
    数据库索引的作用和长处缺点
  • 原文地址:https://www.cnblogs.com/juechen/p/5255996.html
Copyright © 2011-2022 走看看