hdu2818(2009多校第一场) 移动木块问某木块下的木块数(带权并查集)
更新fatber[] down[] up[] 三个数组
求得是down数组
1.忽略合并同一区间 2.下标从0开始2333
1 #include<stdio.h> 2 #include<string.h> 3 int father[100005],down[100005],up[100005]; 4 int find(int x) 5 { 6 if (x==father[x]) return father[x]; 7 int tmp=find(father[x]); 8 down[x]+=down[father[x]]; 9 father[x]=tmp; 10 return tmp; 11 } 12 int main() 13 { 14 int n,i,x,y,f1,f2; 15 char c; 16 while (~scanf("%d",&n)) 17 { 18 for (i=0;i<=n;i++) 19 { 20 father[i]=i; 21 down[i]=0; 22 up[i]=1; 23 } 24 for (i=1;i<=n;i++) 25 { 26 scanf("%*c%c",&c); 27 if (c=='M') 28 { 29 scanf("%d%d",&x,&y); 30 f1=find(x); f2=find(y); 31 if (f1==f2) continue; 32 father[f1]=f2; 33 down[f1]=up[f2]; 34 up[f2]+=up[f1]; 35 } 36 else 37 { 38 scanf("%d",&x); 39 y=find(x); 40 printf("%d\n",down[x]); 41 } 42 } 43 } 44 return 0; 45 }