POJ1703 Find them, Catch them

Posted SilverNebula

tags:

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

 
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 41913   Accepted: 12879

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

 
偏移量并查集……
然而我又用影子集水过去了。
为每个点x开一个影子节点(x+n),表示与x不在同一个集合。每次读到D x y就把x+n和y合并,之后判断就行。
 
 1 /*by SilverN*/
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 using namespace std;
 8 const int mxn=120000;
 9 int fa[mxn<<1];
10 char c[5];
11 int n,m;
12 int find(int x){
13     if(fa[x]==x)return x;
14     return fa[x]=find(fa[x]);
15 }
16 int main(){
17     int T;
18     int i,j;
19     int x,y;
20     scanf("%d",&T);
21     while(T--){
22         scanf("%d%d",&n,&m);
23         int nn=2*n;
24         for(i=1;i<=nn;i++){
25             fa[i]=i;
26         }
27         for(i=1;i<=m;i++){
28             scanf("%s%d%d",c,&x,&y);
29             if(c[0]==D){
30                 x=find(x);//
31                 y=find(y);//AC以后试着优化,删了这两条,居然MLE了
32                 //想来是这两步起了压缩并查集路径的作用? 
33                 int a=find(x+n);
34                 int b=find(y+n);
35                 fa[a]=y;
36                 fa[b]=x;
37                 
38             }
39             else{
40                 if(find(x)==find(y)){
41                     printf("In the same gang.\n");
42                     continue;
43                 }
44                 if(find(x)==find(y+n) || find(y)==find(x+n)){
45                     printf("In different gangs.\n");
46                     continue;
47                 }
48                 printf("Not sure yet.\n");
49             }
50         }
51     }
52     return 0;
53 }

 

以上是关于POJ1703 Find 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

[并查集] POJ 1703 Find them, Catch them