简单并查集
#include < iostream >
#include < cstdio >
#include < cstdlib >
#include < cstring >
using namespace std;
#define maxn 100005
int n, m;
int f[maxn], father[maxn];
int getanc( int a)
{
if (a == father[a])
return a;
return father[a] = getanc(father[a]);
}
void merge( int a, int b)
{
father[getanc(a)] = getanc(b);
}
void differ( int a, int b)
{
if (f[a] == - 1 )
f[a] = b;
else
merge(f[a], b);
if (f[b] == - 1 )
f[b] = a;
else
merge(f[b], a);
}
void ask( int a, int b)
{
if (f[a] == - 1 || f[b] == - 1 )
{
printf( " Not sure yet.\n " );
return ;
}
if (getanc(a) == getanc(b))
{
printf( " In the same gang.\n " );
return ;
}
if (getanc(a) == getanc(f[b]))
{
printf( " In different gangs.\n " );
return ;
}
printf( " Not sure yet.\n " );
}
int main()
{
// freopen("t.txt", "r", stdin);
int t;
scanf( " %d " , & t);
while (t -- )
{
memset(f, - 1 , sizeof (f));
scanf( " %d%d " , & n, & m);
getchar();
for ( int i = 0 ; i < n; i ++ )
father[i] = i;
for ( int i = 0 ; i < m; i ++ )
{
char ch;
int a, b;
scanf( " %c%d%d " , & ch, & a, & b);
a -- ;
b -- ;
getchar();
if (ch == ' D ' )
differ(a, b);
else
ask(a, b);
}
}
return 0 ;
}