Is It A Tree?——是一棵树吗?(并集查)

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

Sample Output

Case 1 is a tree.

Case 2 is a tree.

Case 3 is not a tree.

描述:

树是一种已知的数据结构,它要么是空的(空的,空的,无的),要么是一组由满足以下性质的节点之间的有向边连接的一个或多个节点。
这里正好有一个节点,叫做根,没有有向的边指向它。

除了根,每个节点都有一条边指向它。
从根到每个节点有一个唯一的有向边序列。
例如,考虑下面的插图,其中节点用圆表示,边缘用箭头表示。前两个是树,最后一个不是。

在此问题中,将给出由有向边连接的节点集合的若干描述。对于其中的每一个,您将确定集合是否满足树的定义。

输入:

输入将由一组描述(测试用例)和一对负整数组成。每个测试用例将由一组边的描述和一组零组成,每个边的描述将由一组整数组成;第一个整数标识出边开始的节点,而第二个整数则标识了边缘所指向的节点。节点数总是大于零。

输出:

对于每个测试用例显示“Case k is a tree”。或“Case k is not a tree”。,其中K对应于测试用例数(它们从1开始顺序编号)。

/*  1,无环;
    2,除了根,所有的入度为1,根入度为0;
    3,这个结构只有一个根,不然是森林了。再注意这里空树也是树。 
*/

AC码:

/*1,无环;
2,除了根,所有的入度为1,根入度为0;
3,这个结构只有一个根,不然是森林了。再注意这里空树也是树。 
*/
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int f[100005];//对f[],用1表示父节点,2表示子节点,来标记各个点
int main()
{
	int a,b,flag,i,j;
	int t=1;
	while(1)
	{
		j=0;//结点数 
		i=0;//边数 
		flag=0;
		memset(f,0,sizeof(f));
		while(~scanf("%d %d",&a,&b)&&a&&b)
		{//输入(a->b)
			if(a<0||b<0)
			return 0;//a和b为负数时,结束程序 
			if(f[b]-1==1)//f[b]=2,即b结点已经做过子节点 
			flag=1;//如果b已经作为过子节点,再一次做子节点,说明出现两个父节点(也可能是重复的边,也不是树),不是树
			if(f[a]==0)
			j++;//结点数+1 
			if(f[b]==0)
			j++;//结点数+1
			f[a]=1;
			f[b]=2;
			i++;//边数+1 
		}
		if(flag==0&&j==i+1)//保证有向图所有结点都包涵在内的话,变数和结点数的关系是:节点数=边数+1 
		printf("Case %d is a tree.\n",t++);
		else
		printf("Case %d is not a tree.\n",t++);
	}
	return 0;
	
}

猜你喜欢

转载自blog.csdn.net/hanyue0102/article/details/81661625