程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> NYOJ--樹的判定

NYOJ--樹的判定

編輯:C++入門知識

樹的判定

時間限制:1000 ms | 內存限制:65535 KB 難度:4
描述

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.

輸入
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.

The number of test cases will not more than 20,and the number of the node will not exceed 10000.
The inputs will be ended by a pair of -1.
輸出
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).
樣例輸入
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 #include using std::endl; using std::cin; using std::cout; int Parent[10000]; int Visit[10000]; //並查集,得到父親節點 int getParent(int x) { if(Parent[x]==0) return x; else return getParent(Parent[x]); } int main() { int x,y; int cnt=1; //記錄節點的個數和邊的個數 int vex,edge; while(cin >> x >> y) { //判斷是否為樹的標志 bool flag=true; vex=0; edge=0; //重置數據 memset(Parent,0,sizeof(Parent)); memset(Visit,0,sizeof(Visit)); //終止循環 if(x==-1&&y==-1) break; if(x==y) {//自己到自己的邊,肯定不是樹 flag=false; } if(x!=y) { //節點和邊增加 vex+=2; edge++; Visit[x]=Visit[y]=1; Parent[y]=x; } if(x==0&&y==0) {//第一組數據表明此時為空樹,空樹也是樹 cout << "Case " << cnt <<" is a tree." << endl; cnt++; continue; }else{ while(cin >> x >> y) { if(x==0&&y==0) break; if(x==y) { flag=false; continue; } if(!Visit[x]) { vex++; Visit[x]=1; } if(!Visit[y]) { vex++; Visit[y]=1; } //找x和y的最頂上的祖先節點 int m=getParent(x); int n=getParent(y); //判斷是否為樹 //m==n的情況是出現環狀指向了同一個父親節點的情況,此時組成的邊也可能節點數滿足關系,當此時不是樹 //n!=y的情況是y此時已經有父親節點此時又有一個節點指向了y,也就是樹中不存在入度大於一的節點 if(m==n||n!=y) { flag=false; continue; } Parent[n]=m; edge++; } } if(!flag||edge!=vex-1) { cout << "Case " << cnt << " is not a tree." << endl; cnt++; }else{ cout << "Case " << cnt << " is a tree." << endl; cnt++; } } return 0; }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved