题目传送门
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define Read(x) scanf("%d",&x)
#define maxn 10010
int fa[maxn],num[maxn],time[maxn];
void init()
{
for(int i=0;i<maxn;i++)
{
fa[i]=i;
num[i]=1;
time[i]=0;
}
}
int Find(int x)
{
if(x==fa[x])return x;
int fx=Find(fa[x]);
time[x]+=time[fa[x]];
return fa[x]=fx;
}
void union_set(int x,int y)
{
int fx=Find(x);
int fy=Find(y);
if(fx!=fy)
{
fa[fx]=fy;
num[fy]+=num[fx];
time[fx]++;
}
}
int main()
{
char s[10];
int tt,N,Q,x,y,ncase=1;
Read(tt);
while(tt--)
{
init();
Read(N);Read(Q);
printf("Case %d:\n",ncase++);
while(Q--)
{
scanf("%s",s);
getchar();
if(s[0]=='T')
{
Read(x);Read(y);
union_set(x,y);
}
else
{
Read(x);
y=Find(x);
printf("%d %d %d\n",y,num[y],time[x]);
}
}
}
return 0;
}