POJ1703Find them, Catch them

Posted 嘒彼小星

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ1703Find them, Catch them相关的知识,希望对你有一定的参考价值。

Find them, Catch them
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 47222   Accepted: 14543

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.

Source

 
【题解】
加权并查集裸题,w[i]表示i与其父节点是否相等。
判断两个元素是否想等,先find两个元素,使其合并到
代表元素下。代表元素不等时,关系不确定;相等
时,根据两元素w值判断即可
技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 
 6 inline void read(int &x)
 7 {
 8     x = 0;char ch = getchar(),c = ch;
 9     while(ch < 0 || ch > 9)c = ch, ch = getchar();
10     while(ch <= 9 && ch >= 0)x = x * 10 + ch - 0, ch = getchar();
11     if(c == -)x = -x;
12 }
13 
14 const int MAXN = 100000 + 10;
15 
16 int fa[MAXN], w[MAXN], t, n, m;
17 char s[1];
18 
19 int find(int x)
20 {
21     if(fa[x] == x)return x;
22     int f = find(fa[x]);
23     w[x] = w[fa[x]] ^ w[x];
24     fa[x] = f;
25     return f;
26 }
27 
28 int main()
29 {
30     read(t);
31     register int tmp,tmp1, tmp2, f1, f2;
32     for(;t;-- t)
33     {
34         read(n);read(m);
35         for(register int i = 1;i <= n;++ i)fa[i] = i, w[i] = 0;
36         for(register int i = 1;i <= m;++ i)
37         {
38             scanf("%s", s);
39             if(s[0] == D)
40             {
41                 read(tmp1), read(tmp2);    
42                 f1 = find(tmp1), f2 = find(tmp2);
43                 tmp = w[tmp1] ^ w[tmp2] ^ 1;
44                 fa[f1] = f2, w[f1] = tmp;
45             }
46             else
47             {
48                 read(tmp1), read(tmp2);
49                 f1 = find(tmp1), f2 = find(tmp2);
50                 if(f1 == f2)
51                     if(w[tmp1] == w[tmp2])printf("In the same gang.\n");
52                     else printf("In different gangs.\n");
53                 else printf("Not sure yet.\n");
54             }
55         }
56     }
57     return 0;
58 } 
POJ1703

 

 

以上是关于POJ1703Find them, Catch them的主要内容,如果未能解决你的问题,请参考以下文章

POJ1703Find them, Catch them

POJ1703 Find them, Catch them

poj-1703-Find them, Catch them

poj 1703 Find them, Catch them

(POJ 1703)Find them, Catch them

POJ 1703 Find them,Catch them