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]會偏大,使用鄰接矩陣有可能內存溢出。

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