这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解。
#include <cstring>
#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int o[];
int flag[];
int find(int x)
{
if (x == o[x]) return x;
int t = find(o[x]);
flag[x] = (flag[o[x]] + flag[x]) % ;
return o[x] = t;
}
void merge(int x,int y)
{
int tx,ty;
tx = find(x);
ty = find(y);
if(tx != ty)
{
o[tx] = ty;
flag[tx] = (flag[x]+flag[y]+)%;
}
}
int judge(int x,int y)
{
int tx,ty;
tx = find(x);
ty = find(y);
if(tx != ty)
return -;
else
return (flag[x] == flag[y]);
}
int main()
{
int T,sv,ev,i,n,m;
char ch;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%*c",&n,&m);
for(i = ;i <= n;i ++)
{
flag[i] = ;
o[i] = i;
}
for(i = ;i < m;i ++)
{
scanf("%c%*c%d%d%*c",&ch,&sv,&ev);
if(ch == 'A')
{
int temp = judge(sv,ev);
if(temp == -)
printf("Not sure yet.\n");
else if(temp == )
printf("In different gangs.\n");
else
printf("In the same gang.\n");
}
else
{
merge(sv,ev);
}
}
}
return ;
}