
2 3 3 T 1 2 T 3 2 Q 2 3 4 T 1 2 Q 1 T 1 3 Q 1
Case 1: 2 3 0 Case 2: 2 2 1 3 3 2
//687MS 324K
#include<stdio.h>
#include<string.h>
#define M 10007
struct node
{
int pre,son,tan;
} p[M];
int find(int x)
{
if(p[x].pre==x)return x;
else
{
int xx=p[x].pre;
p[x].pre=find(p[x].pre);
p[x].tan+=p[xx].tan;
}
return p[x].pre;
}
void unio(int a,int b)
{
int x=find(a);
int y=find(b);
if(x==y)return;
p[x].pre=y;
p[x].tan++;
p[y].son+=p[x].son;
p[x].son=0;
}
int main()
{
int t,cas=1;
scanf("%d",&t);
while(t--)
{
int n,q;
scanf("%d%d",&n,&q);
for(int i=1; i<=n; i++)
{
p[i].pre=i;
p[i].son=1;
p[i].tan=0;
}
printf("Case %d:\n",cas++);
int a,b,c;
char s[2];
for(int i=0; i<q; i++)
{
scanf("%s",s);
if(s[0]==‘T‘)
{
scanf("%d%d",&a,&b);
unio(a,b);
}
else
{
scanf("%d",&c);
int ans=find(c);
printf("%d %d %d\n",p[ans].pre,p[ans].son,p[c].tan);
}
}
}
return 0;
}
HDU 3635 Dragon Balls 带权并查集,布布扣,bubuko.com
原文:http://blog.csdn.net/crescent__moon/article/details/25129985