b674: Is It A Tree
Tags :
Accepted rate : 43人/47人 ( 91% ) [非即時]

Content

A tree is a well-known data structure that is either empty or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties.

1. There is exactly one node, called the root, to which no directed edges point.
2. Every node except the root has exactly one edge pointing to it.
3. There is a unique sequence of directed edges from the root to each node.

For example, consider the graphs given below, in which circles represent nodes and lines with arrowheads represent edges. 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

Each test case contains two lines. The first line is the number of edges n, 1 <= n <= 1000, and the second line consists of a sequence of edges. Each edge consists of a pair of integers u and v, 1 <= u, v <= 1000. The first integer u identifies the node from which the edge begins, and the second integer v identifies the node to which the edge is directed. It is easy to see that this line must contain even number of integers. A line with 0 signifies the end of input.

Output

Output 'y' if the test case is a tree; otherwise output 'n'.

Sample Input #1
5
6 8 5 3 5 2 6 4 5 6
8
8 1 7 3 6 2 8 9 7 5 7 4 7 8 7 6
6
3 8 6 8 6 4 5 3 5 6 5 2
0
Sample Output #1
y
y
n

Hint ：

1.邊的數量是否為點的數量減一
2.不存在循環路徑(cycle)
3.所有的點一值追尋其父點都會找到同一點,也就是root

Tags:

[管理者：
(囧rz)
]

 ID User Problem Subject Hit Post Date 沒有發現任何「解題報告」