poj1308 2010.2.8

poj1308 2010.2.8

#include <stdio.h>
#include <string.h>

#define N 100

int hash[N][N],poifa[N],k;
int father,son,num;
int used[N];

int doit();
int find(int);
int match();
int okok();

int okok()
{
	int i,m;
	m=0;
	for(i=1;i<=num;i++)
		if ((used[i])&&(poifa[i]==0)) m++;
	if (m==1) return 1;
	else return 0;
}

int find(int x)
{
	if (poifa[x]==0)
		return x;
	else
		return (poifa[x]=find(poifa[x]));

}

int match()
{
	used[father]=1;
	used[son]=1;
	if (father>num) num=father;
	if (son>num) num=son;
	if (hash[father][son]) return 0;
	if (poifa[son]) return 0;
	hash[father][son]=1;
	hash[son][father]=1;
	if (poifa[father]==son) return 0;
	poifa[son]=find(father);
	return 1;
}
int doit()
{
	if (match())
	{
		while (scanf("%d %d",&father,&son),father)
		{
			if (!match())
				return 0;
		}
		return 1;
	}
	return 0;
}

int main()
{
	k=0;
	int i;
	while (scanf("%d %d",&father,&son),father>=0)
	{
		k++;
		num=0;
		memset(hash,0,sizeof(hash));
		memset(poifa,0,sizeof(poifa));
		memset(used,0,sizeof(used));
		if ((father==0)&&(son==0))
			printf("Case %d is a tree.\n",k);
		else
		{
			if (doit()&&okok())
					printf("Case %d is a tree.\n",k);
				else
				{
					printf("Case %d is not a tree.\n",k);
					if (father)
						while (scanf("%d %d",&father,&son),father);
				}
//			for(i=1;i<=num;i++)
//				printf("%d    %d\n",i,poifa[i]);
		}
	}
	return 0;
}
			


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值