Poj 1308 HDU 1325 is it a tree? [Check the set + inbound to determine whether a directed graph is a tree]

Source: Internet
Author: User

Poj 1308

HDU 1325

Is it a tree?
Time limit:1000 ms   Memory limit:10000 K
Total submissions:17384   Accepted:5956

Description

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

Input

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.

Output

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 ).

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.

Source

North central North America 1997

Question:Determines whether a directed graph is a tree.

Algorithm: And query set + inbound

Note:: Determines whether a vertex is used.

At first, I chose to record the maximum and minimum values, but I couldn't even connect to the sample. Later I output the intermediate variable and found that some points in the middle were useless.

Therefore, we need to open an array used [] to determine whether the subscript has been used.

Tree Definition: 1. All used subscripts are in one connected component.

2. The root node's inbound level is 0, and the inbound level of all other nodes is 1.

Strange: The subscript range is not given, and 1000 has passed.

/* Baccepted240 kb0 MSC ++ 1875 B2013-04-07 19:47:27 */# include <cstdio> # include <cstring> # include <algorithm> using namespace STD; const int maxn = 1000; int P [maxn]; // int R [maxn]; // bool used [maxn]; // determines whether int Max, Min is used for subscript; // determine the topic value void set () used in the tree // initialize {for (INT x = 0; x <maxn; X ++) {P [x] = x; // set the independence degree of each point to 0 used [x] = false for Father's Day; // none at each point} min = maxn; max =-maxn;} int find (INT x) // Find the root node {return x = P [x]? X: P [x] = find (P [x]);} void Union (int x, int y) // merge the tree where two points are located {int FX = find (x); int FY = find (y); P [FY] = FX ;}int main () {int, b; int C = 0; // The group of data in which the record is while (scanf ("% d", & A, & B )! = EOF) {if (a =-1 & B =-1) break; Set (); // initialize P [B] =; R [B] ++; // process the first data in min = min (a, B); max = max (a, B); used [a] = true; // flag use used [B] = true; int X, Y; bool flag = true; while (scanf ("% d", & X, & Y )! = EOF) {If (x = 0 & Y = 0) {int root = 0, index; // record the number of root nodes and subscript for (INT I = min; I <= max; I ++) {If (used [I] & I = P [I]) // Several root {root ++; index = I ;}} if (root! = 1) Flag = false; // a tree can have only one root. If (FLAG) {for (INT I = min; I <= max; I ++) // determine the inbound level. Except the root node, the inbound level of other nodes is 1 {if (I! = Index & used [I] & R [I]! = 1) Flag = false ;}} if (FLAG) printf ("case % d is a tree. \ n ", ++ C); else printf (" case % d is not a tree. \ n ", ++ C); // printf (" % d \ n ", root); // start to output the intermediate variable with some minor errors and find the break error. // note: easy to forget} If (find (x )! = Find (y) Union (x, y); // if not in the same tree, merge R [y] ++; // inbound + 1 used [x] = true; // mark used [y] = true; min = min (Min, x); min = min (Min, y); // update max = max (max, x); max = max (max, Y);} 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.