[Check the set] to determine whether it is a tree

Source: Internet
Author: User

[Problem description]

A tree is a data structure that everyone is familiar with. It may be an empty tree or a set of directed edges connected by nodes that meet the requirements.

A tree has only one root node, and the root node does not point to its edge.

Each node except the root node has only one edge pointing to it.

No graph is a tree.

Determine the set of directed edges connected by some nodes to determine whether the graph composed of input data of each group is a tree.

[Input]

When each input is 0, it indicates that the input of a group of data is complete. Each edge is represented by a positive integer. The first number is the start point of the directed edge, and the second number is the end point of the directed edge. A negative number indicates the end of the input.

[Output]

Each group of test data outputs a row of determination results. If the input graph is a tree, "Case k is a tree." is output; otherwise, "Case k is not a tree." is output .". K indicates the number of each data group (the number starts from 1 ).

[Solution Report]

You can use the query set to determine whether a graph is a tree.

Based on the definition and features of the tree, consider the following situations:

(1) A node in the tree can have at most one parent node;

(2) rings cannot appear in the tree;

(3) A graph consists of only one root node. Otherwise, a forest rather than a tree is formed.

By observing the input and output, we can see that the numbers in the question are displayed immediately. Therefore, you must mark the points that appear in the programming process before you can judge them.

Step 1: Mark each pair of input root nodes, indicating that these nodes have appeared and are operated. During the operation, the two nodes cannot have the same root node. Otherwise, the ring will be formed. If node B is to be connected to node a, ensure that node B is a root node. Otherwise, B will have two parent nodes. If there is no such situation, you can merge the two parent nodes. .Www.2cto.com

Step 2: after each group of data is input, calculate the total number of root nodes in the graph. If the total number of root nodes is not 1, the graph is not a tree.

Step 3: output the results based on the above judgment. initialize the data after each group of results is output.

[Cpp]
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
 
Int father [1000000], flag [1000000];
 
Int findset (int x) // query
{
If (x! = Father [x])
Father [x] = findset (father [x]); // path compression
Return father [x];
}
 
Int unionset (int a, int B) // and
{
Int x = findset ();
Int y = findset (B );
 
If (y! = B) // B is connected to a. Ensure that B is the root node. Otherwise, B will have two parent nodes.
Return 1;
 
If (x = y) // if a and B are in the same tree, the loop is generated if they are executed again.
Return 1;
Else
Father [y] = x; // if no of the preceding conditions exist, you can merge two shards.
Return 0;
}
 
Int main ()
{
Int a, B, key = 0, p = 1, I, max = 0, t = 0;
For (I = 1; I <= 999999; I ++)
Father [I] = I;
 
Memset (flag, 0, sizeof (flag ));
 
While (1)
{
Scanf ("% d", & a, & B );
If (a> max) max =;
If (B> max) max = B;
If (a <=-1 & B <=-1) break;
 
If (a = 0 & B = 0)
{
For (I = 1; I <= max; I ++)
{
If (flag [I] = 1 & father [I] = I)
T ++;
 
If (t> = 2) break;
}
 
If (key> 0 | t> = 2)
Printf ("Case % d is not a tree. \ n", p ++ );
Else
Printf ("Case % d is a tree. \ n", p ++ );
 
For (I = 1; I <= 999999; I ++)
Father [I] = I;
 
Key = 0; t = 0; max = 0;
Memset (flag, 0, sizeof (flag ));
Continue;
}
 
Flag [a] = 1; flag [B] = 1; // ID of the Input
Key + = unionset (a, B); // if the value of the key is invalid, the value of the key is greater than 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.