版本2和3间的区别
于2006-05-04 12:51:08修订的的版本2
大小: 3103
编辑: czk
备注:
于2008-02-23 15:36:59修订的的版本3
大小: 3115
编辑: localhost
备注: converted to 1.6 markup
删除的内容标记成这样。 加入的内容标记成这样。
行号 18: 行号 18:
attachment:1268_1.gif attachment:1268_2.gif attachment:1268_3.gif {{attachment:1268_1.gif}} {{attachment:1268_2.gif}} {{attachment:1268_3.gif}}

Is It A Tree?

http://acm.zju.edu.cn/show_problem.php?pid=1268

Time limit: 1 Seconds

Memory limit: 32768K

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.

1268_1.gif 1268_2.gif 1268_3.gif

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.

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

2. 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).

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

4. Sample Output

Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.

Problem Source: North Central North America 1997


   1 /*written by czk*/
   2 #include <iostream>
   3 #include <map>
   4 using namespace std;
   5 
   6 int main() {
   7     for(int caseno = 1;;caseno++) {
   8         map<int, int> in_degree;
   9         while(true) {
  10             int from, to;
  11             cin >> from >> to;
  12             if(from == -1 && to == -1)
  13                 return 0;
  14             else if(from == 0 && to == 0)
  15                 break;
  16             in_degree.insert(pair<int, int>(from, 0));
  17             in_degree.insert(pair<int, int>(to, 0));
  18             in_degree[to]++;
  19         }
  20         bool result = true;
  21         int root = -1;
  22         for(map<int, int>::iterator iter = in_degree.begin(); iter != in_degree.end(); iter++)
  23             if(iter->second == 0 && root == -1) {
  24                 root = iter->first;
  25             } else if(iter->second > 1 || iter->second == 0 && root != -1) {
  26                 result = false;
  27                 break;
  28             }
  29         if( root == -1 && !in_degree.empty())
  30             result = false;
  31         cout << "Case "<<caseno<<" is "<< (result?"":"not ") << "a tree." << endl;
  32     }
  33 }

zju1268 (2008-02-23 15:36:59由localhost编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.