Find them, Catch them

Posted 只有你

tags:

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

Find them, Catch them

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 70   Accepted Submission(s) : 23
Problem 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
PKU
 
题意:第一行表示用例组数,第二行表示人数和信息组数有两个帮派,D a b表示a和b属于不同帮派,A a b表示要你回答a和b是属于同一帮派、不同帮派还是并不确定。
 
思路:N个人分属于两个帮派。所以我们可以初始化一个N*2的并查集,然后给定D a b就unit(a,b+n),unit(b,a+n)。
 
技术分享
 1 #include <cstdio>
 2 using namespace std;
 3 
 4 const int maxn=200000+10;//最大为N的两倍
 5 int par[maxn];//用来存放它的父结点
 6 
 7 void init(int s)//初始化并查集
 8 {
 9     for(int i=0;i<s;i++)
10     {
11         par[i]=i;
12     }
13 }
14 
15 int find(int x)//找根
16 {
17     if(par[x]==x)
18         return x;
19     else
20     {
21         return par[x]=find(par[x]);//路径压缩,用根代替它的父节点
22     }
23 }
24 
25 bool same(int a,int b)//判断是否属于一个集合
26 {
27     return find(a)==find(b);
28 }
29 
30 void unit(int x,int y)//合并x和y代表的两个集合
31 {
32     int fx=find(x);
33     int fy=find(y);
34     par[fx]=fy;
35 }
36 
37 int main()
38 {
39     int t;
40     scanf("%d",&t);
41     while(t--)
42     {
43         int n,m;
44         scanf("%d%d",&n,&m);
45         init(n*2);//初始化一个大小为本身两倍的并查集
46         for(int i=0;i<m;i++)
47         {
48             char op;
49             int x,y;
50             getchar();
51             scanf("%c%d%d",&op,&x,&y);
52             if(op==D)
53             {
54                 unit(x,y+n);
55                 unit(y,x+n);
56             }
57             else if(op==A)
58             {
59                 if(same(x,y) || same(x+n,y+n))
60                 {
61                     printf("In the same gang.\n");
62                 }
63                 else if(same(x,y+n) || same(y,x+n))
64                 {
65                     printf("In different gangs.\n");
66                 }
67                 else
68                 {
69                     printf("Not sure yet.\n");
70                 }
71             }
72         }
73 
74     }
75     return 0;
76 }
View Code

 









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

poj 1703 Find them, Catch them

ACM Find them, Catch them

ACM题目————Find them, Catch them

Find them, Catch them

POJ1703Find them, Catch them

POJ1703 Find them, Catch them