标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6844 Accepted Submission(s): 1952
1 #include <map> 2 #include <cstring> 3 #include <cstdio> 4 #include <iostream> 5 using namespace std; 6 char a[25], b[25]; int father[200020], num[200020]; 7 map<string, int> mmap; 8 void init() 9 { 10 for(int i = 1; i <200020; i++) 11 { 12 father[i] = i; 13 num[i] =1; 14 } 15 } 16 int find(int a) 17 { 18 while(a != father[a]) 19 a = father[a]; 20 return a; 21 } 22 void mercy(int a, int b) //加了Yh,,过了, 23 { 24 int q = find(a); 25 int p = find(b); 26 if(q != p) 27 { 28 if(num[q] > num[p]) 29 { 30 father[p] = q; 31 num[q] +=num[p]; 32 printf("%d\n", num[q]); 33 } 34 else 35 { 36 father[q] = p; 37 num[p] += num[q]; 38 printf("%d\n", num[p]); 39 } 40 } 41 else 42 printf("%d\n", num[p]); 43 } 44 int main() 45 { 46 int m, t, num; 47 while(cin >> t) 48 { 49 while(t--) 50 { 51 52 init(); num = 1; 53 mmap.clear(); 54 scanf("%d", &m); 55 //for(int i = 1; i <= m; i++) 56 while(m--) 57 { 58 scanf("%s %s", a, b); 59 if(!mmap[a]) mmap[a] = num++; 60 if(!mmap[b]) mmap[b] = num++; 61 mercy(mmap[a], mmap[b]); 62 } 63 } 64 } 65 return 0; 66 }
杭电3172--Virtual Friends(并查集+map)
标签:
原文地址:http://www.cnblogs.com/fengshun/p/4707134.html