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 }

时间: 2024-08-04 12:47:43

POJ1703 Find them, Catch them 并查集 好题 有坑点的相关文章

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

POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集

POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y均能C通信,则x和y可以通信.现在给出若干个操作, O p 代表修复编号为p的电脑 S p q代表询问p和q是不是能通信. 思路: 并查集即可.. 如果修复了一台电脑,则把与它相连距离不超过d的且修复了的放在一个集合里面. #include<cstdio> #include<cstring&

G - Brain Network (easy)(并查集水题)

G - Brain Network (easy) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u CodeForces 690C1 Description One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don't know

poj 1182 食物链 并查集好题

挑战程序设计上有解答的并查集好题.把事件作为元素进行合并,举例:若输入1 2 3,意思就是把2,3归为同一类,至于归于哪一类并不需要去讨论,则把2属于A,3属于A这两件事件归为一类;2属于B,3属于B这两件事归为一类;2属于C,3属于C这两件事归为一类:若输入 2 2 3,由于A吃B,B吃C,C吃A,就把2属于A,3属于B这两件事情归为一类:以此类推.当检测到当前情况与之前正确的情况不符合,则错误的情况数加1. #include <iostream> #include <cstdio&g

【HDU1232】畅通工程(并查集基础题)

裸敲并查集,很水一次AC 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cctype> 6 #include <cmath> 7 #include <algorithm> 8 #include <numeric> 9 #include <string> 1

【HDU1856】More is better(并查集基础题)

裸并查集,但有二坑: 1.需要路径压缩,不写的话会TLE 2.根据题目大意,如果0组男孩合作的话,应该最大的子集元素数目为1.所以res初始化为1即可. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <numeric> 7 #include <

【HDU1325】Is It A Tree?(并查集基础题)

有以下坑点: 1.结束输入不一定-1,题目中的叙述只是说所有权值都为正值. 2.是否构成一棵树不能只判断是否只有一个根节点,没有环路,而且还需要判断每个节点的入度一定是1,不然就不是一棵树. (无环路也可用树的性质:结点数 = 边树 + 1 来取代) 1 #include <iostream> 2 #include <cstdlib> 3 #include <cstring> 4 #include <cctype> 5 #include <cmath&

UVALive 4487 Exclusive-OR 加权并查集神题

已知有 x[0-(n-1)],但是不知道具体的值,题目给定的信息 只有 I P V,说明 Xp=V,或者 I P Q V,说明 Xp ^ Xq=v,然后要求回答每个询问,询问的是 某任意的序列值 Xp1^Xp2,,,,X^pk 这个题目用加权并查集是这么处理的: 1. f[]照样是代表父节点,照样进行路径压缩,把每个 V[i]=V[i]^V[f[i]],即节点存储的值实际是它与它父亲的异或的值.为什么要这样呢,因为异或首先满足交换律,而且异或同一个数偶数次,即相当于本身,那么这个题目的其中一个要

【HDU2120】Ice_cream&#39;s world I(并查集基础题)

查环操作,裸题.一次AC. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cctype> 5 #include <cmath> 6 #include <string> 7 #include <cstdio> 8 #include <algorithm> 9 #include <numeric>