b674. Is It A Tree
標籤 : Graph
通過比率 : 99人/113人 ( 88% ) [非即時]
評分方式:
Tolerant

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

內容

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.

輸入說明

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 'y' if the test case is a tree; otherwise output 'n'.

範例輸入 #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
範例輸出 #1
y
y
n
測資資訊:
記憶體限制: 64 MB
提示 :

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

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

本題狀況 本題討論 排行

編號 身分 題目 主題 人氣 發表日期
39635 eric22221213 ... (遊戲亡 gd) b674
112 2024-03-14 20:15