Find them, Catch them
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 31532 | Accepted: 9713 |
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.
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.
题意:警察要肃清两个帮派,该城市有龙帮和蛇帮两个帮派,给出两个罪犯,让我们判断是否属于一个帮派。
当前有N(1-N)个罪犯在这个城市,至少他们其中一个属于龙帮或者蛇帮,给出M行信息,信息分两种:
1. D [a] [b] :a和b罪犯不属于同一个帮派。
2. A [a] [b] :判断a和b是不是在一个帮派。是就输出In the same gang.不是就输出In different gangs.不能判断 就输出Not sure yet.
分析:种类并查集;
两个数组,一个为储存根节点的数组,另外一个为储存该点与根节点之间的关系(0,或者1)在寻找根节点的时候需要更新 该点与根节点之间的关系;x与y合并的时候,若x1为y1的根节点,那么需要求ans[y1];
ans[y1]=(ans[x]-ans[y]+1)%2;如下图所示可得出
#include #include int ans[100010];//储存该节点与根节点的关系 int father[100010];//储存根节点 int find(int x) { if(x==father[x])return x; int k=father[x]; father[x]=find(father[x]); ans[x]=(ans[x]+ans[k])%2;//更新该节点与根节点之间的关系 return father[x]; } void cal(int x,int y) { int x1=find(x); int y1=find(y); if(x1!=y1) { father[y1]=x1; ans[y1]=(ans[x]-ans[y]+1)%2;//合并的时候求出ans[y1] } } int main() { int t; int n,m; scanf("%d",&t); char a[2]; int b,c; while(t--) { scanf("%d%d",&n,&m); for(int i=0;i<=n;i++) { father[i]=i; ans[i]=0; } while(m--) { scanf("%s",&a); scanf("%d%d",&b,&c); if(a[0]=='A') { int x1=find(b); int y1=find(c); if(x1==y1)//如果根节点相同的话说明该点出现过,合并过了 { if(ans[b]==ans[c])//如果这两个节点均与根节点的关系相同,那么属于一个帮派 printf("In the same gang.\n"); else printf("In different gangs.\n"); } else printf("Not sure yet.\n"); } else cal(b,c); } } return 0; }