Find them, Catch them
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 17373 | Accepted: 5105 |
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.
Source
POJ Monthly--2004.07.18
这道题做的实属不易啊,研究了一个晚上,第二天早上才做出来,弄得我一晚上都没睡好,就是简单并查集的一个升级,需要存储偏移量(就是与父亲节点的关系(一类,敌对))
代码和测试数据:
#include<stdio.h>
int flag[100008],map[100008];
int father(int x)
{
int t;
if(map[x]==x)
return map[x];
t=map[x];
map[x]=father(map[x]);
flag[x]=(flag[t]+flag[x])%2;
return map[x];
}
int main()
{
int t,m,n,i,ra,rb,a,b;
char s;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=0;i<=n;i++)
{
map[i]=i;
flag[i]=0;
}
if(n==2){flag[2]=1;map[2]=1;}
while(m--)
{
getchar();
scanf("%c %d %d",&s,&a,&b);
ra=father(a);
rb=father(b);
if(s=='A'){
if(ra==rb){
if(flag[a]==flag[b])printf("In the same gang.\n");
else printf("In different gangs.\n");
}
else printf("Not sure yet.\n");
}
else{
if(ra<rb)
{
map[rb]=ra;
flag[rb]=!((flag[a]+flag[b])%2);
}
else {
map[ra]=rb;
flag[ra]=!((flag[a]+flag[b])%2);
}
}
}
}
return 0;
}
测试数据(带图示箭头表示敌对):
1
11 30
A 1 2
Not sure yet.
D 1 3
D 4 5
A 1 4
Not sure yet.
D 3 5
A 1 4
In different gangs.
A 1 5
In the same gang.
D 3 5
D 7 9
D 2 7
A 1 2
Not sure yet.
D 1 7
A 1 9
In the same gang.
A 3 7
In the same gang.
A 5 7
In different gangs.
A 2 5
In the same gang.
A 2 4
In different gangs.
A 9 5
In the same gang.
D 8 10
D 6 8
A 6 10
In the same gang.
D 10 11
A 8 11
In the same gang.
D 1 8
A 1 11
In different gangs.
A 4 11
In the same gang.
A 9 11
In different gangs.
A 2 6
In the same gang.
A 3 6
In different gangs.
A 1 1
In the same gang.
这道题做的实属不易啊,研究了一个晚上,第二天早上才做出来,弄得我一晚上都没睡好,就是简单并查集的一个升级,需要存储偏移量(就是与父亲节点的关系(一类,敌对))
代码和测试数据:
#include<stdio.h>
int flag[100008],map[100008];
int father(int x)
{
}
int main()
{
}
测试数据(带图示箭头表示敌对):

1
11 30
A 1 2
Not sure yet.
D 1 3
D 4 5
A 1 4
Not sure yet.
D 3 5
A 1 4
In different gangs.
A 1 5
In the same gang.
D 3 5
D 7 9
D 2 7
A 1 2
Not sure yet.
D 1 7
A 1 9
In the same gang.
A 3 7
In the same gang.
A 5 7
In different gangs.
A 2 5
In the same gang.
A 2 4
In different gangs.
A 9 5
In the same gang.
D 8 10
D 6 8
A 6 10
In the same gang.
D 10 11
A 8 11
In the same gang.
D 1 8
A 1 11
In different gangs.
A 4 11
In the same gang.
A 9 11
In different gangs.
A 2 6
In the same gang.
A 3 6
In different gangs.
A 1 1
In the same gang.