POJ 1703

来源:互联网 发布:中科大软件学院招生 编辑:程序博客网 时间:2024/05/17 02:18
Find them, Catch them
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 27592 Accepted: 8416

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.大致意思是给你两个帮派 D X Y 代表 XY不属于一个帮派 A X Y 让你输出这两个人之间有何者关系,网上说这题叫做种类并查集,需要对节点进行更新保存操作还是怎么的,表示战五渣看不懂,后面就多开了一个数组记录成员所属的帮派信息,以及该成员的敌对人员是谁,所以D操作实际上是把两个人分开的操作,比如D 1 2 代表 1 2属于不同帮派然后记录1的对手是2 2的对手是1 ,然后再加入D 2 4  那么就说明 2还有个对手是4 那么显然 1 4是同一个帮派的 所以合并1 4 故在A 1 4时 输出他们是一个帮派的。大致就是如此。
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<cmath>using namespace std;int pre[100005];int rank[100005];int n,m;void init(){    int i;    for(i=1;i<=n;i++)    {        pre[i]=i;        rank[i]=0;//一开始都没有敌对的人    }}int find(int x){    if(x==pre[x])        return pre[x];    pre[x]=find(pre[x]);    return pre[x];}void join(int x,int y){    int fx=find(x);    int fy=find(y);    if(fx!=fy)        pre[fx]=fy;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&m);        getchar();        init();        for(int i=0;i<m;i++)        {            int c=getchar();            getchar();            int a,b;            scanf("%d%d",&a,&b);            getchar();            int p=find(a);            int q=find(b);            if(c=='A')            {                if(p==q)//先祖相同 必然同宗。                {                    printf("In the same gang.\n");                }                else                {                    if(rank[p]==0)//如果先祖不同,而且P的敌对没人,代表现在二者间的关系不明朗                    {                        printf("Not sure yet.\n");                    }                    else                    {                        if(find(rank[p])==q)//因为RANK【P】记录的是P的敌对之人,如果P的敌对之人的                        {                   //先祖 跟加进来的Q相同,那么P Q肯定是不同帮派的                            printf("In different gangs.\n");                        }                        else                        {                            printf("Not sure yet.\n");//否则还是不确定                        }                    }                }            }            if(c=='D')            {                if(rank[p]!=0) join(find(rank[p]),q);//如果P的敌人已经出现,那么合并P敌人的先祖和Q                if(rank[q]!=0) join(find(rank[q]),p);//如果Q的敌人已经出现,那么合并Q敌人的先祖和P                if(rank[p]==0) rank[p]=q;//如果加进来的时候没敌人,那么互相更新 P的敌人是Q                if(rank[q]==0) rank[q]=p;//Q的敌人是P。            }        }    }    return 0;}


0 0
原创粉丝点击