POJ 1703 Find them, Catch them

来源:互联网 发布:linux常用网络命令大全 编辑:程序博客网 时间:2024/05/19 03:16
Find them, Catch them
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 24693 Accepted: 7418

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

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

Sample Output

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

Source

POJ Monthly--2004.07.18
 花了一下午的时间,终于有思路了。
其基本思路是 假设已知集合A和B的人不是同一伙的,集合C和D的人也不是一伙的。那么如果现在再告诉我们A和C的人不在一伙,那么我们就合并集合A和D, B和C合并
集合A存集合B的根,集合B存集合A的根 即root[x] = y; root[y]=x;
给定两个数x1和y1,找他们的跟分别为x和y
(1)x==y 则是一伙的
(2)root[x]==y&&root[y]==x则不是一伙的
(3)不满足(1)(2) 则不能确定
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#define N 100010using namespace std;bool visit[N];char s1[2];int a[N],root[N];int main(){    //freopen("data1.in","r",stdin);    int find(int x);    int t;    scanf("%d",&t);    while(t--)    {        int n,m;        scanf("%d %d",&n,&m);        for(int i=1;i<=n;i++)        {            a[i] = i;        }        if(n==2)        {            while(m--)            {                int x,y;                scanf("%s %d %d",s1,&x,&y);                if(s1[0]=='A')                {                    printf("In different gangs.\n");                }            }            continue;        }        memset(visit,false,sizeof(visit));        while(m--)        {            int x,y;            scanf("%s %d %d",s1,&x,&y);            int root_x,root_y;            if(s1[0]=='A')            {                root_x = find(x);                root_y = find(y);                if(root_x == root_y)                {                    printf("In the same gang.\n");                }else if(root[root_x]==root_y&&root[root_y]==root_x)                {                    printf("In different gangs.\n");                }else                {                    printf("Not sure yet.\n");                }            }else if(s1[0]=='D')            {                if(!visit[x]&&!visit[y])                {                    root[x] = y;                    root[y] = x;                    visit[x] = visit[y] = true;                }else if(visit[x]&&!visit[y])                {                    root_x = find(x);                    root_y = root[root_x];                    a[y] = root_y;                    visit[y] = true;                }else if(!visit[x]&&visit[y])                {                    root_y = find(y);                    root_x = root[root_y];                    a[x] = root_x;                    visit[x] = true;                }else                {                    int root_x1 = find(x);                    int root_y1 = root[root_x1];                    int root_x2 = find(y);                    int root_y2 = root[root_x2];                    a[root_y2] = root_x1;                    a[root_x2] = root_y1;                }            }        }    }    return 0;}int find(int x){    int k1,k2;    k1 = x;    while(k1!=a[k1])    {        k1 = a[k1];    }    while(x!=a[x])    {        k2 = a[x];        a[x] = k1;        x = k2;    }    return k1;}