标签:
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 21157 | Accepted: 7395 | |
Case Time Limit: 1000MS |
Description
Input
Output
Sample Input
6 M 1 6 C 1 M 2 4 M 2 6 C 3 C 4
Sample Output
1 0 2
1 #include<stdio.h> 2 #define N 30001 3 4 int count[N], num[N], pre[N]; 5 6 void inite() 7 { 8 for(int i = 0; i < N; i++) 9 { 10 count[i] = 0; 11 num[i] = 1; 12 pre[i] = i; 13 } 14 } 15 16 int find(int x) 17 { 18 if(pre[x] == x) 19 return x; 20 21 int t = find(pre[x]); 22 count[x] += count[pre[x]]; 23 pre[x] = t; 24 return t; 25 26 } 27 void Union(int x, int y) 28 { 29 int i = find(x); 30 int j = find(y); 31 if(i == j) 32 { 33 return; 34 } 35 count[i] = num[j]; 36 num[j] += num[i]; 37 pre[i] = j; 38 } 39 40 41 42 int main() 43 { 44 int i, x, y, n; 45 char s[2]; 46 scanf("%d",&n); 47 inite(); 48 for(i = 0; i < n; i++) 49 { 50 scanf("%s",s); 51 if(s[0] == ‘M‘) 52 { 53 scanf("%d%d",&x,&y); 54 Union(x,y); 55 } 56 else if(s[0] == ‘C‘) 57 { 58 scanf("%d",&x); 59 int c = find(x); 60 printf("%d\n",count[x]); 61 } 62 } 63 return 0; 64 }
标签:
原文地址:http://www.cnblogs.com/cjshuang/p/4678789.html