Is it a tree?
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 10559 accepted submission (s): 2429 problem descriptiona 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 should t 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 specified strations 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.
Inputthe 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.
Outputfor 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 ).
Sample Input
6 8 5 3 5 2 6 45 6 0 08 1 7 3 6 2 8 9 7 57 4 7 8 7 6 0 03 8 6 8 6 45 3 5 6 5 2 0 0-1 -1
Sample output
Case 1 is a tree.Case 2 is a tree.Case 3 is not a tree.
Determine whether each group of data can constitute a tree.
1. Each tree has only one root node and does not point to its edge (that is, the root node's inbound degree is 0 );
2. Except the root node, other nodes have only one edge pointing to it (that is, the inbound degree of other nodes is 1 );
3. A loop cannot be formed, that is, a loop cannot be generated.
Import Java. io. *; import Java. util. *; public class main {int max = 1100005, Count = 1, head = 0; Boolean Bo; Boolean boo [] = new Boolean [Max]; int Patten [] = new int [Max]; int in [] = new int [Max]; public static void main (string [] ARGs) {New Main (). work ();} void work () {vertex SC = new vertex (New bufferedinputstream (system. in); Init (); While (SC. hasnext () {int A = SC. nextint (); int B = SC. nextin T (); if (a <0 & B <0) break; if (a = 0 & B = 0) {for (INT I = 0; I <Max; I ++) {If (boo [I]) {head = find (I); // find the root node break;} For (INT I = 0; I <Max; I ++) {If (boo [I]) {If (in [I]> 1) {// except for the root node, the inbound level of other nodes cannot be greater than 1 BO = false; break;} If (Head! = Find (I) {// each tree has only one root node BO = false; break ;}} if (BO) system. out. println ("case" + Count ++ "is a tree. "); else system. out. println ("case" + Count ++ "is not a tree. "); Init () ;}else {boo [a] = true; boo [B] = true; If (find (A) = find (B )) // The loop cannot contain BO = false; else {Union (a, B); in [B] ++ ;}}// merge void Union (int, int B) {int Pa = find (a); int Pb = find (B); If (Pa = Pb) return; Patten [PA] = Pb ;} // search for int find (int x) {If (Patten [x] = x) return X; Patten [x] = find (Patten [x]); // return Patten [X];} // initialize void Init () {BO = true; head = 0; arrays. fill (in, 0); arrays. fill (Boo, false); For (INT I = 1; I <Max; I ++) {Patten [I] = I ;}}}