标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 35979 Accepted Submission(s):
19068
4 2 1 3 4 3 3 3 1 2 1 3 2 3 5 2 1 2 3 5 999 0 0
1 0 2 998
Huge input, scanf is recommended.
1 #include <stdio.h> 2 int city[10010]; 3 4 int find (int a) 5 { 6 if(city[a]!=a) 7 city[a]=find(city[a]); 8 return city[a]; 9 } 10 11 int main() 12 { 13 int m,n,i,a,b; 14 while(~scanf("%d",&m),m) 15 { 16 scanf("%d",&n); 17 for(i=1; i<=m; i++) 18 city[i]=i; 19 for(i=1; i<=n; i++) 20 { 21 scanf("%d %d",&a,&b); 22 a=find(a); 23 b=find(b); 24 city[a]=b; 25 } 26 int add_street=-1; 27 for(i=1;i<=m;i++) 28 { 29 if(city[i] == i) 30 add_street++; 31 } 32 printf("%d\n",add_street); 33 } 34 return 0; 35 }
标签:
原文地址:http://www.cnblogs.com/fengshun/p/4609188.html