hdu 2818 Building Block
#include<stdio.h>
struct node
{
int parent;
int up;
int all;
} n[30000+5];
int Find(int x)
{
if(x!=n[x].parent)
{
int t=Find(n[x].parent);
n[x].up+=n[n[x].parent].up;
n[x].parent=t;
}
return n[x].parent;
}
void Union(int x,int y)
{
int fx=Find(x),fy=Find(y);
if(fx!=fy){
n[fy].parent=fx;
n[fy].up=n[fx].all;
n[fx].all+=n[fy].all;
}
}
int main()
{
int i,p,x,y;
char c[10];
scanf("%d",&p);
for(i=0; i<30000+5; i++)
{
n[i].parent=i;
n[i].all=1;
n[i].up=0;
}
while(p--)
{
scanf("%s",&c);
if(c[0]=='M')
{
scanf("%d%d",&x,&y);
Union(x,y);
}
else if(c[0]=='C')
{
scanf("%d",&x);
int t=Find(x);
printf("%d\n",n[t].all-1-n[x].up);
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/