HDU 1325 is it a tree? (Query set)

Source: Internet
Author: User
Is it a tree?
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 421 Accepted submission (s): 157
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 failed 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
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.

Output
For each test case display the line ''case K is
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.
 

See poj (1038)

ProgramAnalysis (reproduced ):

This question is very similar to Xiao Xi's maze. It is just an undirected graph and a directed graph. It also gives you the information between those nodes, and then let you determine whether it is a tree. Let's use the definition of the tree to determine whether there is no ring. N nodes can have up to n-1 edges, otherwise there will be loops. There is only one node with an inbound value of 0, and there is no node with an inbound value greater than 1. This is enough to judge whether it is a tree. But pay attention to some special data situations. The empty tree is also a tree. For example, input 0.

Solution:

In fact, you do not need to check the set. In this way, you can directly calculate the set according to the preceding conditions to determine whether it is a tree.

Self-Comments: Well, the key to this question is to judge the standards of the tree, 1, no ring; 2, except for the root, all the inbound degrees are 1, and the root inbound degrees are 0; 3, this structure has only one root, or it is a forest.

These are three standards. Note that the empty tree here is also a tree. Basically, it's OK ..

It's the bottomCodeIt can be passed on HDU, but not on poj ..... After reading discuss, we know that 1 1 0 0 cannot be a tree.

Pay attention to the following situations ....

 
1: 0 0 empty tree is a tree 2: 1 0 0 not a tree itself cannot point to itself 3: 1 2 2 0 0 not a tree .... I started to be so depressed and repetitive in this wa ~~ 55554: 1 2 2 3 4 5 not a tree forest, not a tree (mainly pay attention to yourself) 5: 1 2 2 3 3 4 4 5 6 6 7 8 8 9 9 1 note that a node pointing to its father or ancestor is wrong, namely 9 --> 1 Error 6: 1 2 2 1 0 0 is also incorrect
In this way, you can basically pass the poj question. poj (1038)

This is the Code passed on HDU:
View code

# Include <stdio. h> Const   Int Max_num = 100000 + 10  ; Typedef  Struct  {  Int Num, root, Conn;//  Data, root, and inbound  } Node; node [max_num];  Void  Init (){  For ( Int I = 0 ; I <max_num; I ++ ) {Node [I]. Conn = 0 ; //  The inbound Initialization is 0. Node [I]. Root = I; //  The root record is itself Node [I]. num =0 ; //  Indicates whether a number has been used, 0: not used, 1: Used  }}  Int Find_root ( Int  A ){  If (Node [A]. Root! = A)  Return Node [A]. Root = Find_root (node [A]. Root );  Return  Node [A]. root ;}  Void Union_set (Int A, Int  B) { = Find_root (a); B = Find_root (B );  If (A = B) //  The same root indicates that it is under the same tree.      Return  ; Node [B]. Root =; //  Assign the root of B to the root of A. At this time, a is already the root, num = root  }  Int  Main (){ Int  N, m;  Int I = 1  ;  Bool Flag = True ; //  True: A tree; false: Not a tree  Init ();  While (Scanf ( "  % D  " , & N, & M )! = EOF & n> = 0 & M> = 0  ){  If (! Flag & n! = 0 & N! = 0 ) Continue ; //  If it is determined that it is not a tree, continue the loop.          If (N = 0 & M = 0  ){  Int Root_num = 0 ;  For ( Int J = 1 ; J <max_num; j ++ ){  //  Determines whether it is a forest. If root_num is used to record the root number                  If (Node [J]. Num & find_root (j) = J) root_num ++ ;  If (Node [J]. Conn> 1 ) // If the inbound traffic of a node exceeds 1  {Flag = False  ;  Break  ;}}  If (Root_num> 1 ) //  The connected branch is greater than 1, indicating that the forest is not a tree. Flag = False  ;  If (FLAG) printf (  "  Case % d is a tree. \ n  " , I ++ );  Else Printf ( "  Case % d is not a tree. \ n  " , I ++ ); Flag = True  ; Init ();  Continue  ;} If (M! = N & find_root (n) = Find_root (M) Flag = False  ;  Else  {  //  Record M, N as a node Node [M]. num = 1  ; Node [N]. Num = 1  ; Node [M]. Conn ++; //  1 more inbound Union_set (n, m );}}  Return   0  ;} 

In the middle, just make a slight modificationIf(M! = N & find_root (n) =Find_root (m ))ChangedIf(M! = N & find_root (n) =Find_root (m) | M = N) passed on poj

 

View code

# Include <stdio. h> Const   Int Max_num = 100000 + 10 ; Typedef  Struct  {  Int Num, root, Conn; //  Data, root, and inbound  } Node; node [max_num];  Void  Init (){  For ( Int I = 0 ; I <max_num; I ++ ) {Node [I]. Conn = 0 ;//  The inbound Initialization is 0. Node [I]. Root = I; //  The root record is itself Node [I]. num = 0 ; //  Indicates whether a number has been used, 0: not used, 1: Used  }}  Int Find_root ( Int  A ){  If (Node [A]. Root! = A)  Return Node [A]. Root =Find_root (node [A]. Root );  Return  Node [A]. root ;}  Void Union_set ( Int A, Int  B) { = Find_root (a); B = Find_root (B );  If (A = B) //  The same root indicates that it is under the same tree.      Return  ; Node [B]. Root =;//  Assign the root of B to the root of A. At this time, a is already the root, num = root  }  Int  Main (){  Int  N, m;  Int I = 1  ;  Bool Flag = True ; //  True: A tree; false: Not a tree  Init ();  While (Scanf ( "  % D  " , & N, & M )! = EOF & n> = 0 & M> = 0  ){  If (! Flag & n! = 0 & N! = 0 ) Continue ; //  If it is determined that it is not a tree, continue the loop.          If (N = 0 & M = 0  ){  Int Root_num = 0  ;  For ( Int J = 1 ; J <max_num; j ++ ){  //  Determines whether it is a forest. If root_num is used to record the root number                  If (Node [J]. Num & find_root (j) = J) root_num ++;  If (Node [J]. Conn> 1 ) //  If the inbound traffic of a node exceeds 1  {Flag = False  ;  Break  ;}}  If (Root_num> 1 ) //  The connected branch is greater than 1, indicating that the forest is not a tree. Flag = False  ;  If  (FLAG) printf (  "  Case % d is a tree. \ n  " , I ++ );  Else Printf ( "  Case % d is not a tree. \ n  " , I ++ ); Flag = True ; Init ();  Continue  ;}  If (M! = N & find_root (n) = find_root (M) | M = N) Flag = False  ;  Else  {  //  Record M, N as a node Node [M]. num = 1  ; Node [N]. Num =1  ; Node [M]. Conn ++; //  1 more inbound  Union_set (n, m );}}  Return   0  ;} 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.