poj-Is IT A Tree

 

描述

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.

 

输入

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.

输出

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).

样例输入

6 8  5 3  5 2  6 4
5 6  0 0

8 1  7 3  6 2  8 9  7 5
7 4  7 8  7 6  0 0

3 8  6 8  6 4
5 3  5 6  5 2  0 0
-1 -1

样例输出

Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.

 

解题思路:

 

根据题目里面的三个条件:可将其转化为,如果一颗树是树,那么他满足:

(1)根节点的入度为0,说明在所有节点中,入度为0的点只有一个

(2)任何节点除了根点外都有节点指向他,说明整个图中连通分量只有一支

(3)从根节点到任何节点只有一条路径,说明除了根节点之外的其他节点的入度全为1,且只能为1

注意:空树也是树......

# include<stdio.h>
# include<memory.h>

int tree[500];
int in[500];//入度
bool used[500];//used用来判断所有节点是否第一次出现
int point[500];//point用来保存第一次出现的所有点
int find(int x)
{
	if(tree[x]==x)
		return x;
	return find(tree[x]);
}
struct E
{
	int a,b;
}ed[500];
int main()
{
	int a,b,i,Case=0;
	for(i=0;i<500;i++)
	{
		in[i]=0;
		tree[i]=i;
		point[i]=-1;
		used[i]=false;
	}
	int index=0; bool flag=false;
	int num=0;
	while(scanf("%d%d",&a,&b)!=EOF)
	{
		if(a<0&&b<0)  break;
		if(a!=0||b!=0)
		{
			if(used[a]==false){
				used[a]=true;
				point[num++]=a;
			}
			if(used[b]==false){
				used[b]=true;
				point[num++]=b;
			}
			ed[index].a=a;
			ed[index].b=b;
			index++;
			in[b]++;
		}

		else if(a==0&&b==0)
		{
			Case++;
			int cnt=0,cntVex=0,cntDu=0;
			if(num==0)//这个地方是个坑,num=0说明是空树,空树也是树
			{
				printf("Case %d is a tree.\n",Case);
				continue;
			}

			for(i=0;i<index;i++)//并查集的思想
			{ 
				int m=find(ed[i].a);
				int n=find(ed[i].b);
				if(m!=n)
					tree[m]=n;
			}

			for(i=0;i<num;i++)
			{
				if(tree[point[i]]==point[i])//统计整个图中的连通分支数
					cnt++;
				if(in[point[i]]==0)//入度为0的节点,只能有一个
					cntVex++;
				else if(in[point[i]]>1)//除根节点的其余节点,入度均为1,且只能为1
					cntDu++;
			}

			if(cnt==1&&cntVex==1&&cntDu==0)
				printf("Case %d is a tree.\n",Case);
			else
				printf("Case %d is not a tree.\n",Case);
			//下一次初始化
			for(i=0;i<500;i++)
			{
				in[i]=0;
				tree[i]=i;
				point[i]=-1;
				used[i]=false;
			}
			index=0;flag=false;num=0;
		}	
	}
	return 0;
}

 

posted @ 2018-04-28 21:18  xzhws  阅读(41)  评论(0编辑  收藏  举报