ZOJ1268(Is It A Tree?)

Is It A Tree?

Time Limit: 1 Second      Memory Limit: 32768 KB

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//2009-05-18 10:04:15 Accepted  1268 C++ 0 
  2#include <iostream>
  3#include <set>
  4using namespace std;
  5
  6const int N = 1004;
  7
  8typedef struct
  9{
 10    int parent;
 11    int height;
 12}
node;
 13
 14node tree[N];
 15bool hp[N];//hp[i] =  true表示结点i有父亲结点
 16
 17void init()
 18{
 19    for(int i = 1; i < N; i++)
 20    {
 21        tree[i].parent = i;
 22        tree[i].height = 1;
 23        hp[i] = false;
 24    }

 25}

 26
 27int find(int x)
 28{
 29    while(x != tree[x].parent)
 30        x = tree[x].parent;
 31    return x;
 32}

 33
 34void merge(int x, int y)
 35{
 36    if(tree[x].height == tree[y].height)
 37    {
 38        tree[y].parent = x;
 39        tree[x].height++;
 40    }

 41    else if(tree[x].height < tree[y].height)
 42    {
 43        tree[y].parent = x;
 44    }

 45    else
 46    {
 47        tree[x].parent = y;
 48    }

 49}

 50
 51
 52
 53int main()
 54{
 55    int a, b, a1, b1, T = 1;
 56    bool flag, mark;
 57    set<int> SI;
 58    set<int>::iterator p;
 59
 60    while(scanf("%d%d"&a, &b) != EOF)
 61    {
 62        if(a < 0)
 63            break;
 64        if(!&& !b)
 65        {
 66            printf("Case %d is a tree.\n",T++);
 67            continue;
 68        }

 69
 70        SI.clear();
 71        init();
 72        flag = true; mark = false;
 73
 74        while(1)
 75        {
 76            if(mark)
 77                scanf("%d%d"&a, &b);
 78            mark = true;
 79            if(!&& !b)
 80                break;
 81            if(flag)
 82            {
 83                if(hp[b])
 84                {//b已有父亲结点
 85                    flag = false;
 86                    continue;
 87                }

 88
 89                hp[b] = true;
 90                a1 = find(a);
 91                b1 = find(b);
 92
 93                if(a1 == b1)
 94                {//存在环
 95                    flag = false;
 96                    continue;
 97                }

 98
 99                SI.insert(a);
100                SI.insert(b);
101                merge(a1, b1);
102            }

103        }

104        
105        if(flag)
106        {//判连通
107            p = SI.begin();
108            a = find(*p);
109
110            for(p++; p != SI.end(); p++)
111                if(a != find(*p))
112                {
113                    flag = false;
114                    break;
115                }

116        }

117
118        printf("Case %d is ",T++);
119        if(flag)
120            printf("a tree.\n");
121        else
122            printf("not a tree.\n");
123    }

124    return 0;
125}

126

posted on 2009-05-18 10:06  Xredman  阅读(235)  评论(0编辑  收藏  举报

导航