题目1012:畅通工程(树的并查集以及路径优化)
题目链接:http://ac.jobdu.com/problem.php?pid=1012
详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus
参考代码:
// // 1012 畅通工程.cpp // Jobdu // // Created by PengFei_Zheng on 17/04/2017. // Copyright © 2017 PengFei_Zheng. All rights reserved. // #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <cmath> #define MAX_SIZE 1010 using namespace std; int n, m; int Tree[MAX_SIZE]; int findRoot(int x){//find the root of x if(Tree[x]==-1) return x; else{ int tmp = findRoot(Tree[x]);//continue the find Tree[x] = tmp;//change the root of x to tmp return tmp; } } int main(){ while(scanf("%d",&n)!=EOF && n!=0){//when n==0 and jump out of the loop scanf("%d",&m); for(int i = 1 ; i <= n ; i++)//init Tree[i] = -1; while(m--){ int a,b; scanf("%d%d",&a,&b); a = findRoot(a);//find the root of a b = findRoot(b);//find the root of b if(a!=b) Tree[a]=b;//merge those two sets to the same aggregates } int ans = 0 ; for(int i = 1 ; i <= n ; i++){ if(Tree[i]==-1) ans++;//calculate the total numbers of aggregates } printf("%d\n",ans-1); } return 0; } /************************************************************** Problem: 1012 User: zpfbuaa Language: C++ Result: Accepted Time:10 ms Memory:1524 kb ****************************************************************/
作者: 伊甸一点
出处: http://www.cnblogs.com/zpfbuaa/
本文版权归作者伊甸一点所有,欢迎转载和商用(须保留此段声明),且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
原文链接 如有问题, 可邮件(zpflyfe@163.com)咨询.