zoukankan      html  css  js  c++  java
  • POJ1703(2集合并查集)

    Find them, Catch them
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 39402   Accepted: 12101

    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.

    /*
        @ title : poj1703
        @ method : union-find sets
        @ result : Accepted    1948K    813MS    G++    967B
        @ auther : baneHunter
        @ date : 2016/3/19
    */
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int MAXN=100005;
    int par[MAXN+MAXN],rnk[MAXN+MAXN];
    int n,m;
    void prep()
    {
        for(int i=0;i<MAXN+MAXN;i++)
        {
            par[i]=i;
            rnk[i]=0;
        }
    }
    
    int fnd(int x)
    {
        if(x==par[x])
            return x;
        return par[x]=fnd(par[x]);
    }
    
    void unite(int x,int y)
    {
        int a=fnd(x);
        int b=fnd(y);
        if(a==b)    return ;
        if(rnk[a]<rnk[b])
        {
            par[a]=b;
        }    
        else
        {
            par[b]=a;
            if(rnk[a]==rnk[b])
                rnk[a]++;
        }
    }
    
    bool same(int x,int y)
    {
        return fnd(x)==fnd(y);
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d",&n,&m);
            prep();
            while(m--)
            {
                scanf("%*c");
                char op;
                int x,y;
                scanf("%c",&op);
                scanf("%d%d",&x,&y);
                if(op=='A')    
                {
                    if(same(x,y))
                    {
                        printf("In the same gang.
    ");
                    }
                    else if(same(x,y+n))
                    {
                        printf("In different gangs.
    ");
                    }
                    else    printf("Not sure yet.
    ");
                }
                else
                {
                    unite(x+n,y);
                    unite(x,y+n);
                }
            }
        }
        
        return 0;
    }
  • 相关阅读:
    【转载】ios11 相册读写权限 变更
    【原创】2016年2月14日之后 打ipa包或者提交苹果AppStore失败 证书状态提示“此证书的签发者无效”的解决方法
    【转载】iOS麦克风录制音频并保存成mp3文件的Demo
    【原创】苹果内置录屏SDK-ReplayKit库的使用说明
    iOS sdwebimage 特殊图片错误的问题
    iOS 中 UITextView 限制最大输入字数
    iOS中对NSArray中自定义的对象进行排序
    安卓仿照微信
    Mapped Statements collection already contains value for ***.***的问题
    CSS3之calc()和box-sizing属性
  • 原文地址:https://www.cnblogs.com/program-ccc/p/4986062.html
Copyright © 2011-2022 走看看