Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 22213 | Accepted: 6624 |
Description
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
Output
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.
代码:
#include<stdio.h>
#define N 200050
int n,m;
int set[N];
void init()
{
for(int i=0;i<N;i++)
set[i]=i;
}
int find(int x)
{
return x==set[x]?set[x]: set[x]=find(set[x]) ;
}
bool fun(int a,int b)
{
a=find(a);
b=find(b);
if(a==b)
return false;
set[a]=b;
return true;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
scanf("%d%d",&n,&m);
char s[5];
int a,b;
for(int i=0;i<m;i++)
{
scanf("%s%d%d",s,&a,&b);
if(s[0]=='A')
{
if( find(a)==find(b) )
printf("In the same gang.\n");
else if( find(a)==find(b+n) )
printf("In different gangs.\n");
else
printf("Not sure yet.\n");
}
else
{
fun(a,b+n);
fun(a+n,b);
}
}
}
return 0;
}