首页 > 其他 > 详细

hdu 1232 畅通工程

时间:2014-02-17 12:12:02      阅读:263      评论:0      收藏:0      [点我收藏+]
      赤裸裸的并查集,我还是习惯那位大牛写的方法,首先写出两个数组:father[i]=i和son[i]=1,然后依次查找元素并且使用路径压缩+权值压缩
#include<iostream>
#include<cstdio>
using namespace std;
#define MAX 1000
int son[MAX];
int father[MAX];
int find(int x)
{
	return x == father[x] ? x : find(father[x]);
}
void Union(int x,int y)
{
	int root1=find(x);
	int root2=find(y);
	   if(son[root1]>=son[root2])
	{
		father[root2]=root1;
		son[root1]+=son[root2];
	}
	else
	{
		father[root1]=root2;
		son[root2]=root1;
	}
}

int main()
{
//	freopen("1232.txt","r",stdin);
	int i,n,k,a,b;//n城市数目,k道路条数,a,b道路条数起始点
	while(1)
	{
		int count=-1;
		scanf("%d",&n);
		if(n==0)
			break;
		for(i=1;i<=n;i++)
		{
			father[i]=i;
			son[i]=1;
		}
		scanf("%d",&k);
		for(i=0;i<k;i++)
		{
			scanf("%d%d",&a,&b);
			  Union(a,b);
		}
		for(i=1;i<=n;i++)
		{
			if(father[i]==i)
			{
				count++;
			}
		}
		printf("%d\n",count);

	}
		
		return 0;
	
}
  在kruskal中正是运用的并查集的方法实现两个集合的合并

hdu 1232 畅通工程

原文:http://blog.csdn.net/yuzibo747/article/details/19297003

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!