标签:des style blog http color java os io
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 30798 Accepted Submission(s):
16193
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 5 int bin[1005]; 6 7 int find(int r){ 8 while(r != bin[r]){ 9 r = bin[r]; 10 } 11 return r; 12 } 13 14 int memge(int x, int y){ 15 x = find(x); 16 y = find(y); 17 if(x != y){ 18 bin[x] = y; 19 } 20 return 0; 21 } 22 23 int main(){ 24 int n, m, i, x, y, cout; 25 while(scanf("%d", &n) && n){ 26 scanf("%d", &m); 27 for(i = 1; i <= n; i ++){ 28 bin[i] = i; 29 } 30 for(i = 0; i < m; i ++){ 31 scanf("%d %d", &x, &y); 32 memge(x, y); 33 } 34 cout = -1; 35 for(i = 1; i <= n; i ++){ 36 if(i == bin[i]){ 37 cout ++; 38 } 39 } 40 printf("%d\n", cout); 41 } 42 return 0; 43 }
标签:des style blog http color java os io
原文地址:http://www.cnblogs.com/xiaoyeye/p/3917442.html