标签:des style blog http color os java io strong
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 31088 Accepted Submission(s):
16354
题目大意:按照要求,输出最少需要建的道路条数。间接和直接使道路相连均可。并查集,相当于模板吧,不多说,详见代码。
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 int father[1010],sum; 5 void set(int n) 6 { 7 for (int i=1; i<=n; i++) 8 father[i]=i; 9 } 10 int find(int a) 11 { 12 if (father[a]==a) 13 return a; 14 return father[a]=find(father[a]); 15 } 16 void Union(int x,int y) 17 { 18 x=find(x); 19 y=find(y); 20 if (x!=y) 21 { 22 sum--; 23 father[x]=y; 24 } 25 } 26 int main () 27 { 28 int n,m; 29 while (scanf("%d",&n),n) 30 { 31 scanf ("%d",&m); 32 set(n); 33 sum=n-1; 34 while (m--) 35 { 36 int a,b; 37 scanf("%d%d",&a,&b); 38 Union(a,b); 39 } 40 printf ("%d\n",sum); 41 } 42 return 0; 43 }
标签:des style blog http color os java io strong
原文地址:http://www.cnblogs.com/qq-star/p/3938124.html