http://poj.org/problem?id=1308
方法:
给定一些节点和对应边,判断他们所形成的图是否是树
技巧:类似于“并查集”的表示方法来表示树的结构
tree[1...49]初始为0。tree[i]表示i的父节点。
isNode[1...49]记录哪些编号的节点出现在图中
依次读入父节点p、子节点c和对应边,将isNode[p]和isNode[c]置位1,并赋值tree[c]=p。
如果某赋值时刻tree[c]不为0,意味着节点c已经有一个父节点了,此时该节点将有多于1个的父节
点,所以该图不是树。
如果读入所有数据后不存在多于一个父节点的节点,此时则需要判断该图是否只有一个根节点。方
法即判断图中所有的节点中父节点编号为0的节点是否只有一个。若只有一个则证明只有一个根节
点,若多于一个则证明此图是森林而不是树。
注意:
1、题中未给出节点编号范围,但是经测试肯定在50以内
2、注意空树0 0的情况
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.
1:
2: #include <stdio.h>
3:
4: void run1308()
5: {
6: int i ;
7: int casenum ;
8: int parent,child ;
9: bool isTree ;
10: int rootnum ;
11:
12: casenum = 0 ;
13: while( scanf( "%d%d", &parent,&child ) && parent>=0 )
14: {
15: char tree[50]={0} ; //tree[i]表示i的父节点编号
16: char isNode[50]={0} ; //isNode[i]=1表示图中由此节点,=0表示无此节点
17:
18: isTree = true ;
19: ++casenum ;
20: if( parent==0 ) //空树
21: {
22: printf( "Case %d is a tree.\n", casenum ) ;
23: continue ;
24: }
25:
26: tree[child] = parent ;
27: isNode[child] = 1 ;
28: isNode[parent] = 1 ;
29:
30: while( scanf( "%d%d", &parent,&child ) && parent!=0 )
31: {
32: if( tree[child]!=0 ) //一个孩子有多个父节点
33: isTree = false ;
34: tree[child] = parent ;
35: isNode[child] = 1 ;
36: isNode[parent] = 1 ;
37: }
38:
39:
40: if( !isTree ) //一个孩子有多个父节点
41: printf( "Case %d is not a tree.\n", casenum ) ;
42: else
43: {
44: //判断该图是否只有一个根节点
45: rootnum = 0 ;
46: for( i=1 ; i<50 ; ++i )
47: {
48: if( isNode[i]==1 )
49: {
50: if( tree[i]==0 )
51: ++rootnum ;
52: }
53: }
54: if( rootnum==1 )
55: printf( "Case %d is a tree.\n", casenum ) ;
56: else
57: printf( "Case %d is not a tree.\n", casenum ) ;
58: }
59: }
60: }