标签:div php scan 交通 畅通工程 ++ content 用例 show
题目网址: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): 55211 Accepted Submission(s): 29427
1 #include <cstdio> 2 int n,m; 3 int pre[1005]; 4 void init(){ 5 for (int i=1; i<=n; i++) { 6 pre[i]=i; 7 } 8 } 9 int find(int x){ 10 int r=x; 11 while (pre[x]!=x) { 12 x=pre[x]; 13 } 14 pre[r]=x; 15 return x; 16 } 17 void join(int x,int y){ 18 int fx=find(x),fy=find(y); 19 if(fx!=fy){ 20 pre[fx]=fy; 21 } 22 } 23 int main(){ 24 while (scanf("%d",&n)!=EOF && n) { 25 int res=-1; //当只有一个连通块时,不需要修路,所以res初始化为-1 26 init(); 27 scanf("%d",&m); 28 for (int i=0; i<m; i++) { 29 int a,b; 30 scanf("%d%d",&a,&b); 31 join(a, b); 32 } 33 for (int i=1; i<=n; i++) { 34 if(pre[i]==i) res++; 35 } 36 printf("%d\n",res); 37 } 38 return 0; 39 }
标签:div php scan 交通 畅通工程 ++ content 用例 show
原文地址:http://www.cnblogs.com/uniles/p/7210919.html