码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-04-16 01:07:09      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:

 
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

 

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

标签:

原文地址:http://www.cnblogs.com/-maybe/p/4430656.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!