hdu1325 Is It A Tree? 判断是否为树 并查集

Is It A Tree?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24034    Accepted Submission(s): 5491


Problem Description
A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties.
There is exactly one node, called the root, to which no directed edges point.

Every node except the root has exactly one edge pointing to it.

There is a unique sequence of directed edges from the root to each node.

For example, consider the illustrations below, in which nodes are represented by circles and edges are represented by lines with arrowheads. The first two of these are trees, but the last is not.



In this problem you will be given several descriptions of collections of nodes connected by directed edges. For each of these you are to determine if the collection satisfies the definition of a tree or not.

 

Input
The input will consist of a sequence of descriptions (test cases) followed by a pair of negative integers. Each test case will consist of a sequence of edge descriptions followed by a pair of zeroes Each edge description will consist of a pair of integers; the first integer identifies the node from which the edge begins, and the second integer identifies the node to which the edge is directed. Node numbers will always be greater than zero.
输入将包含一系列描述(测试用例),后跟一对负整数。 每个测试用例将包括一系列边缘描述,后跟一对零每个边缘描述将由一对整数组成; 第一个整数标识边缘开始的节点,第二个整数标识边缘所针对的节点。 节点号将始终大于零。
 

Output
For each test case display the line ``Case k is a tree." or the line ``Case k is not a tree.", where k corresponds to the test case number (they are sequentially numbered starting with 1).
 

Sample Input
 
 
6 8 5 3 5 2 6 45 6 0 08 1 7 3 6 2 8 9 7 57 4 7 8 7 6 0 03 8 6 8 6 45 3 5 6 5 2 0 0-1 -1
 
Sample Output
 
 
Case 1 is a tree.Case 2 is a tree.Case 3 is not a tree.
 
/*
1,无环
2,除了根,所有的入度为1,根入度为0
3,这个结构只有一个根,不然是森林了
注意:空树也是树
*/
#include<stdio.h>
const int max_num=100000+10;
typedef struct
{
	int num,root,conn;//数据,根结点,入度
}Node;

Node node[max_num];

void init()//初始化
{
	for(int i=0;i<max_num;i++)
	{
		node[i].conn=0;//入度初始化为0
		node[i].root=i;//根结点为自身
		node[i].num=0;//标记数字是否用过。0-没用过,1-用过
	}
}

int find(int a)//寻找它的根节点
{
	if(node[a].root!=a)
	return node[a].root=find(node[a].root);
	else
	return node[a].root;
}

void merge(int a,int b)//合并
{
	a=find(a),b=find(b);
	if(a==b)//同一个根,说明在同一棵树里
	return;
	node[b].root=a;//把b的根结点赋给a的根结点,此时a已经是根结点
}

int main()
{
	int n,m,i=1;
	bool flag=true;
	init();
	while(scanf("%d%d",&n,&m)!=EOF&&n>=0&&m>=0)
	{
		if(!flag&&n!=0&&m!=0)continue;//判断是否是树
		if(n==0&&m==0)//说明输入结束,开始处理吧 
		{
			int root_num=0;
			for(int j=1;j<max_num;j++)
			{
				//判断是否为森林。如果是,用root_num来记录根的数目
				if(node[j].num&&find(j)==j)
				root_num++;
				if(node[j].conn>1)//如果出现某个节点的入度超过1,则不是树
				{
					flag=false;
					break;
				}
			}
			if(root_num>1)//连通分支大于1,是森林,不是树
			flag=false;
			if(flag)
			printf("Case %d is a tree.\n",i++);
            else printf("Case %d is not a tree.\n",i++);
            init();
            continue;
		}
		if(m!=n&&find(n)==find(m)||m==n)
		flag=false;
		else
		{
			//将m,n记为结点
			node[m].num=1;
			node[n].num=1;
			node[m].conn++;//入度加一
			merge(n,m);
		}
	}
	return 0;
}




判断是否为树
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值