Is It A Tree?,判断是否是一棵树。(题目来源:九度OJ 1481,2012年北京大学计算机研究生机试真题)

题目描述:

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 and less than 10000.

输出:

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.
来源:

2012年北京大学计算机研究生机试真题

我的思路:

       并查集的使用。

       需要注意一下的情况:

              1.空树

      2.不同路径之间出现通路

      3.出现环路

      4.重复路径(在我下面的代码中没有体现出来,当时我估计数据应该不会这么恶心,但是pdu还是poj上面还真的有重复的路径的case)

以下是AC代码:

#include"stdio.h"
#include"string.h"

int s,e;
int node[10000];
int root[10000];
int flag,num = 1;
int empty;

void initial()
{
	int i;
	flag = 0;
	empty = 1;//判断是否为空树的标记
	memset(node,0,sizeof(node));
	for(i = 0 ; i < 10000 ; ++i)
		root[i] = i;
}

int getRoot(int n,int m)
{
	if(root[n] != n){
		if(root[n] == m) return 0;//如果在查找父节点过程中(s,e)路径中的e,即是出现环路。
		n = root[n];
	}
	return root[n];
}

void join(int s,int e)
{
	int a = getRoot(s,e);//查找父结点的时候,有没有出现环路。
	int b = getRoot(e,0);
	if(!a){//出现环路
		flag = 1;
		return ;
	}
	empty = 0;//只要有路径出现,就不可能是空树
	node[s] = node[e] = 1;
	if((b != a && b != e) || s == e) flag = 1;//flag标记指向自己的路径或者不同路径之间存在一条通路的错误
	else root[e] = a;
}

int judge()
{
	int r = 0;
	int i;
	if(empty) return 1;//空树也是树
	if(flag == 1) return 0;
	for(i = 1; i < 10000 ; ++i)//获得树根结点
	{
		if(node[i]) {
			r = getRoot(i,0);
			break;
		}
	}
	for(; i < 10000 ; ++i)
	{
		if(!node[i]) continue;
		if(r != getRoot(i,0)) return 0;
	}
	return 1;
}

int main()
{
	initial();
	while(~scanf("%d%d",&s,&e))
	{
		if(s && e)
			join(s,e);
		else if(!s && !e) {
			if(judge())
				printf("Case %d is a tree.\n",num++);
			else printf("Case %d is not a tree.\n",num++);
			initial();
		}
		else if(s == -1 && e == -1) break;
	}
	return 0;
}

附另一种思路:

DFS判断,DFS的过程中,可以判断是否出现环路。我感觉这题的数据量假如设int G[10000][10000]会偏大,使用邻接矩阵有可能内存溢出。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章