hdu 1232 畅通工程 最小生成树 并查集

1232的连接:http://acm.hdu.edu.cn/showproblem.php?pid=1232

#include <iostream>
#include <cstdio>
#define MAX 1005
using namespace std;
int father[MAX],rank[MAX];
int N,M;

void make_set(){
for(int i=1;i<=N;i++){
father[i]=i;
}
}
int find_set(int x){
if(x!=father[x]){
father[x]=find_set(father[x]);
}
return father[x];
}

void Union(int x,int y){
x=find_set(x);
y=find_set(y);
if(x==y) return;
father[y]=x;
}
int main()
{
int st,end;
int count;
freopen("acm.txt","r",stdin);
cin>>N>>M;
while(N){
count=0;
make_set();
for(int i=1;i<=M;i++){
cin>>st>>end;
Union(st,end);
}
for(int j=1;j<=N;j++){
if(father[j]!=j)
count++;
}
cout<<N-1-count<<endl; //每合并一次,就可以减少建一条路
cin>>N>>M;
}
return 0;
}


做了几题同类型的并查集的题目。感觉还不错哦~凡事要多练!

转载于:https://www.cnblogs.com/Jason-Damon/archive/2012/01/24/2329137.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值