HDU 1232 -畅通工程(并查集)
生活随笔
收集整理的這篇文章主要介紹了
HDU 1232 -畅通工程(并查集)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目
http://acm.hdu.edu.cn/showproblem.php?pid=1232
代碼
#include <iostream> #include <algorithm> #include <cstring> using namespace std; const int MAXN=1e3 + 100; int pre[MAXN],a[MAXN]; int find(int x) {if(pre[x]==x) return x;return pre[x]=find(pre[x]); } void join(int x,int y) {int fx=find(x),fy=find(y);if(fx!=fy) pre[fy]=fx; } int main() {int n,i,k;long long a,b,m;while(cin>>n){if(n==0) break;cin>>m;for(i=1;i<=n;i++)pre[i]=i;while(m--){cin>>a>>b;join(a,b);}long long ans=0;for(i=1;i<=n;i++){if(pre[i]==i) ans++;//如果一個數(城鎮)的父節點是它本身,則說明與其他路不通 }cout<<ans-1<<endl;}return 0; }總結
以上是生活随笔為你收集整理的HDU 1232 -畅通工程(并查集)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ 1611 -The Suspec
- 下一篇: Codeblocks配置EGE图形库