http://poj.org/problem?id=1988
题意:把集合cubX放到cubY上方,求任意元素的下方有多少元素
分析:主要是计算每个元素与根节点之间的距离
#include<iostream>
#include<cstdio>
using namespace std;
const int N=30005;
int father[N],rank[N],num[N];
int Find(int x)
{
if(x!=father[x])
{
int t=father[x];
father[x]=Find(t);//1)
num[x]+=num[t];//2)__为神马1)和2)换过以后WA啊??
}
return father[x];
}
int main()
{
char cc[2];
int P,X,Y,i,t1,t2;
scanf("%d",&P);
for(i=0;i<=N;i++)
{father[i]=i;rank[i]=1;num[i]=0;}
while(P--)
{
scanf("%s",cc);
if(cc[0]=='M')
{
scanf("%d%d",&X,&Y);
t1=Find(X),t2=Find(Y);
if(t1!=t2)
{
father[t2]=t1;
num[t2]+=rank[t1];//
rank[t1]+=rank[t2];
}
}
else
{
scanf("%d",&X);
t1=Find(X);
printf("%d\n",rank[t1]-num[X]-1);
}
}
return 0;
}