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<cstdio>
#include<algorithm>
using namespace std;
#define maxn 200050
int pa[maxn];
int root(int x)
{
return pa[x]==x?x:pa[x]=root(pa[x]);
}
void mer(int x,int y)
{
int rx=root(x),ry=root(y);
if(rx!=ry)
pa[rx]=ry;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=2*n;i++)
pa[i]=i;
for(int i=1;i<=m;i++)
{
char c[2];
int a,b;
scanf("%s%d%d",c,&a,&b);
if(c[0]=='A')
{
if(root(a)==root(b))
printf("In the same gang.\n");
else if(root(a)==root(b+n)||root(b)==root(a+n))
printf("In different gangs.\n");
else
printf("Not sure yet.\n");
}
else
{
mer(a,b+n);
mer(b,a+n);
}
}
}
return 0;
}
1007



被折叠的 条评论
为什么被折叠?



