基础带权并查集
#include <cstdio>
using namespace std;
const int N=1e4+10;
int fa[N],num[N],cnt[N];
int get(int x){
if(x==fa[x])
return x;
int tmp=fa[x];
fa[x]=get(fa[x]);
cnt[x]+=cnt[tmp];
return fa[x];
}
void Unite(int x,int y){
int fx=get(x),fy=get(y);
if(fx!=fy){
fa[fx]=fy;
num[fy]+=num[fx];
++cnt[fx];
}
}
int main(){
int T,cas=0;scanf("%d",&T);
while(T--){
printf("Case %d:\n",++cas);
int n,q;scanf("%d %d",&n,&q);
for(int i=1;i<=n;++i){
fa[i]=i;num[i]=1;cnt[i]=0;
}
while(q--){
char s;scanf(" %c",&s);
if(s=='T'){
int x,y;scanf("%d %d",&x,&y);
Unite(x,y);
}
else{
int x;scanf("%d",&x);
int t=get(x);
printf("%d %d %d\n",t,num[t],cnt[x]);
}
}
}
return 0;
}