POJ1703 Find them, Catch them 并查集 好题 有坑点

 
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.

 

题意:Tadu City 里面有2个黑帮团伙,一共有n名成员(不过不知道属于哪个帮派),现在警察局有一些信息,每条信息包含2个人编号,表示这2个人属于不用的帮派

问,给出2个人,以目前知道的信息能不能确定他们是否属于同一个帮派。

 

思路:只要2人的关系确定了,就把他们放入同一个集合里面,另外增加一个数组,表示关系,

rel[i] 表示i与其父亲节点的关系,0表示同一个帮派,1表示不同的帮派。

初始化为0

 

2个坑点, 

1.这n个人不能同时属于同一个帮派,所以若现在只剩下2个集合,即使

编号a,b的祖先节点不一样,我们也可以确定a和b 是 In different gangs.

2.写在注释里了。

 

 

 1 #include<cstdio>
 2 
 3 const int maxn=100000+10;
 4 int father[maxn];
 5 int rel[maxn];
 6 int n;
 7 int tot;    
 8 
 9 void make_set()
10 {
11     for(int i=1;i<=n;i++)
12     {
13         father[i]=i;
14         rel[i]=0;      //0表示和father是同类
15     }
16 }
17 int find_set(int x)
18 {
19     if(father[x]==x)
20         return x;
21     else{
22         int prefather=father[x];
23         father[x]=find_set(father[x]);
24         if(rel[x]!=rel[prefather])
25             rel[x]=1;
26         else
27             rel[x]=0;
28         return father[x];
29     }
30 }
31 void union_set(int x,int y)
32 {
33     int fax=find_set(x);
34     int fay=find_set(y);
35     if(fax==fay)
36         return ;
37     father[fax]=fay;
38     //刚开始我是直接rel[fax]=1
39     //这样错了,因为题目说的是x和y确定不是同一个帮派
40     //但是fax和fay仍然有可能是同一个帮派
41     //所以rel[fax]的值要进行判断
42     rel[fax]=(rel[x]==rel[y])?1:0;
43     tot--;                   //记录剩下的集合数
44 }
45 
46 int main()
47 {
48     int test;
49     scanf("%d",&test);
50     while(test--)
51     {
52         int m;
53         scanf("%d%d",&n,&m);
54         char str[3];
55         int a,b;
56         make_set();
57         tot=n;
58         for(int i=1;i<=m;i++)
59         {
60             scanf("%s",&str);
61             if(str[0]=='D')
62             {
63                 scanf("%d%d",&a,&b);
64                 union_set(a,b);
65             }
66             else{
67                 scanf("%d%d",&a,&b);
68                 int faa=find_set(a);
69                 int fab=find_set(b);
70                 if(faa!=fab)
71                 {
72                     if(tot>2)
73                         printf("Not sure yet.\n");
74                     else
75                         printf("In different gangs.\n");
76                 }
77                 else{
78                     if(rel[a]!=rel[b])
79                         printf("In different gangs.\n");
80                     else
81                         printf("In the same gang.\n");
82                 }
83             }
84         }
85     }
86     return 0;
87 }
View Code

 

 

 

POJ2492 和这道题是差不多的思路的,附ac代码

这道题每个输出之后还要一个空行,题目没有说,但是要。

 

 1 #include<cstdio>
 2 
 3 const int maxn=2010;
 4 int n,m;
 5 int father[maxn];
 6 int rel[maxn];
 7 bool flag;
 8 
 9 void make_set()
10 {
11     for(int i=1;i<=n;i++)
12     {
13         father[i]=i;
14         rel[i]=0;
15     }
16     flag=false;
17 }
18 
19 int find_set(int x)
20 {
21     if(father[x]==x)
22         return x;
23     else{
24         int prefather=father[x];
25         father[x]=find_set(father[x]);
26         //原来我是错写成if(father[x]==father[prefather])
27         //这道题半个钟头写完,检查了一个半钟才发现这里
28         if(rel[x]==rel[prefather])
29             rel[x]=0;
30         else
31             rel[x]=1;
32         return father[x];
33     }
34 }
35 
36 void union_set(int x,int y)
37 {
38     int fax=find_set(x);
39     int fay=find_set(y);
40     if(fax==fay)
41     {
42         if(rel[x]==rel[y])
43             flag=true;
44     }
45     else{
46         father[fax]=fay;
47         rel[fax]=rel[x]==rel[y]?1:0;
48     }
49 }
50 
51 int main()
52 {
53     int test;
54     int cas=0;
55     scanf("%d",&test);
56     while(test--)
57     {
58         scanf("%d%d",&n,&m);
59         make_set();
60         cas++;
61         int a,b;
62         for(int i=1;i<=m;i++)
63         {
64             scanf("%d%d",&a,&b);
65             if(!flag)
66             {
67                 union_set(a,b);
68             }
69         }
70         if(flag)
71             printf("Scenario #%d:\nSuspicious bugs found!\n\n",cas);
72         else
73             printf("Scenario #%d:\nNo suspicious bugs found!\n\n",cas);
74     }
75     return 0;
76 }
750ms

 

posted on 2015-04-15 23:26  _fukua  阅读(263)  评论(0编辑  收藏  举报