标签:
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 33284 | Accepted: 10258 |
Description
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<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
int tt,n,m,fa[100010],a[100010];
char s[5];
int find(int x)
{
return x==fa[x]?x:fa[x]=find(fa[x]);
}
void Union(int x,int y)
{
int fx,fy;
fx=find(x),fy=find(y);
if(fx!=fy)
fa[fx]=fy;
}
int main()
{
int x,y;
scanf("%d",&tt);
while(tt--)
{
memset(a,0,sizeof(a));
scanf("%d%d",&n,&m);
for(int i=0;i<=n;i++)
fa[i]=i;
for(int i=1;i<=m;i++)
{
scanf("%s",s);
if(s[0]==‘D‘)
{
scanf("%d%d",&x,&y);
if(a[x]==0)
{
a[x]=y;
}
else
{
Union(a[x],y);
}
if(a[y]==0)
{
a[y]=x;
}
else
{
Union(a[y],x);
}
}
else if(s[0]==‘A‘)
{
scanf("%d%d",&x,&y);
int fx,fy;
fx=find(x),fy=find(y);
if(fx==fy)
{
printf("In the same gang.\n");
}
else if(fx==find(a[y]))
{
printf("In different gangs.\n");
}
else
{
printf("Not sure yet.\n");
}
}
}
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/a972290869/p/4264474.html