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.
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
Case 1 is a tree. Case 2 is a tree. Case 3 is not a tree.
POJ
解析:輸入的是兩個整數為一對每個整數表示節點編號,同時這兩個節點之間有一條邊存在,且是由第一個節點指向第二個節點,英文的大致內容就是去判定輸入的內容是否能組成一顆樹。
首先直接輸入0 0表明此時是一顆空樹,應該輸出is a tree.
當出現節點自己到自己之間的邊時,應該輸出 is not a tree.
如果輸入的邊與節點的數量不滿足edge=vex-1的話也是不屬於樹的
另外當輸入的樹中出現環的話也是不屬於樹
以上這些情況都要考慮到。
其實自己也是看了其他人的博客自己寫的代碼。
開始本人是這樣考慮的,要想組成一顆樹,滿足了節點和邊的條件外,只要輸入的節點中只有一個節點的入度是0,其他的節點的入度是零就OK了(有同學跟我一樣這麼想麼?)
如果按照上面這樣想的話我畫個草圖就明白自己為什麼wa了哈
上面這個草圖滿足節點和邊的條件,也滿足入度出度的情況,但是很明顯這不是一顆樹。<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+06a4w8rH08O1vbKisum8r7XE1qrKtijX1Ly6ttTV4rXju7nDu9PQye6/zLXEwO294ik8L3A+CjxwPrP9wcu/vMLHv9XK97rNvdq149Prsd/WrrzktcS52M+1zeKjrLu50qq1w7W9yuTI68O/zPWx38G9uPa92rXjtcTX7ralyc+1xLi4x9e92rXj0rK+zcrHsqKy6byv1tC1xEZpbmS6r8r9o6zOqsqyw7TSqtXiw7TX9sDgo7/S8s6qyOe5+3ijrHnBvbj2vdq147XEuLi92rXjz+DNrNTyv8+2qLK7ysfK96Os0vLOqru3vs3Kx9Xi1tbH6b/2oaPNrMqxu7nSqrzssul5vdq147XEuLi92rXjyseyu8rHsb7J7aOs0rK+zcrHy7V41rjP8rXE1eK49r3ateMoeSnKx7fx0tG+rdPQvdq149a4z/LBy8v8o6zI57n709C92rXj1rjP8sHLy/yjrLTLyrHU2dPQvdq149a4z/LL/NTyzqW3tMHLyvfW0Mjrtsi089PaMbXEuebU8qOsv8m8+7Kisum8r8rHtuDDtLXE09DTw9PQ0KehozwvcD4KPHA+zca89rnY09qyorLpvK+1xNK7uPayqc7EaHR0cDovL2hpLmJhaWR1LmNvbS9jenl1YW5fYWNtL2l0ZW0vMTNjYmQzMjU4YzI5ZTIwZDcyODYzZWRmKGN6eXVhbtSttLQpPC9wPgo8cD7PwsPmzPnSu8/C19S8urXEtPrC6zwvcD4KPHA+PHByZSBjbGFzcz0="brush:java;">#include