首页 > 其他 > 详细

poj 2524 Ubiquitous Religions

时间:2014-02-20 14:25:06      阅读:328      评论:0      收藏:0      [点我收藏+]

  没什么可说的,我自己写的都一次通过了.

   自己写的代码还要注意细节.

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
#define MAX 50000
#define MAXN 1300000000
int father[MAX],son[MAX];
long long i;
void Uset(int n)
{
	for(i=1;i<=n;i++)
	{
		father[i]=i;
		son[i]=1;
	}
}
int find(int x)
{
	return (x == father[x] ? x : find(father[x]) );
}
bool Union(long long  x,long long y)//有问题啊
{
	long long root1=find(x);
	long long root2=find(y);
	if(root1 == root2)
		return false;
	else if(son[root1] >= son[root2])
	{
		father[root2]=root1;
		son[root1]+=son[root2];

	}
	else
	{
		father[root1]=root2;
		son[root2]+=son[root1];
	}
	return true;

}
int main()
{
//	freopen("1232.txt","r",stdin);
	long long n,m,a,b,count;
	int kcase=0;//?
	while(1)
	{
		scanf("%lld%lld",&n,&m);
		Uset(n);
		if(n==0 && m==0)
			break;
		count=0;
		while(m--)
		{
			scanf("%lld%lld",&a,&b);
//		Union(a,b);
			if(Union(a,b))
				++count;


		}
		kcase++;
		printf("Case %d: %lld\n",kcase,n-count);
//		printf("son[1]=%d\n",son[1]);
	}
	
	return 0;

}


poj 2524 Ubiquitous Religions

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

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