标签: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): 30637 Accepted Submission(s): 16101
1 #include<cstdio> 2 #include<cstring> 3 #include<stdlib.h> 4 #include<algorithm> 5 using namespace std; 6 int p[1005]; 7 int Find(int x) 8 { 9 return p[x]==x?x:p[x]=Find(p[x]); 10 } 11 int main() 12 { 13 //freopen("in.txt","r",stdin); 14 int n,m; 15 while(scanf("%d %d",&n,&m)&&n) 16 { 17 for(int i=1;i<=n;i++) 18 p[i]=i; 19 for(int i=1;i<=m;i++) 20 { 21 int a,b; 22 scanf("%d %d",&a,&b); 23 int x=Find(a); 24 int y=Find(b); 25 if(x!=y) 26 p[x]=y; 27 } 28 int cnt=0; 29 for(int i=1;i<=n;i++) 30 if(p[i]==i) 31 cnt++; 32 printf("%d\n",cnt-1); 33 } 34 return 0; 35 }
HDU 1232 畅通工程 (并查集),布布扣,bubuko.com
标签:des style blog http color java os io
原文地址:http://www.cnblogs.com/clliff/p/3903034.html