b674: Is It A Tree
Tags : Graph
Accepted rate : 43人/47人 ( 91% ) [非即時]
評分方式:
Tolerant

最近更新 : 2015-10-21 13:19

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
測資資訊:
記憶體限制: 64 MB
公開 測資點#0 (100%): 1.0s , <1K
Hint :

這是圖論的題目,確認一個組有向邊是否成為樹,只須確認幾點
1.邊的數量是否為點的數量減一
2.不存在循環路徑(cycle)
3.所有的點一值追尋其父點都會找到同一點,也就是root

Tags:
Graph
出處:
SEARCC-ISSC國際學生程式設計競賽 [管理者:
spocktsai (囧rz)
]


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