Hdoj1325 Is It A Tree ?, Hdoj1325tree

Source: Internet
Author: User

Hdoj1325 Is It A Tree ?, Hdoj1325tree

Is It A Tree? Question Link

Question:
Multiple groups of test data, each group of data has multiple number pairs, indicating a directed edge (that is, the first number is the parent node of the second number), ending with 0, 0 as a group of test data. The test ends when the input is-1 and-1. Determine from the given information whether it is a tree.
Analysis:
1. There can be only one root node, or an empty tree with no points;
2. Except the root node, each vertex has only one parent node.
3. Because there can only be one parent node, We can merge a valid relationship pair (a Father's Day vertex and a child node pair) into a set, their Parent nodes are marked as the root node of the tree to which they belong, so that we can easily determine whether a ring is formed.
4. Determine whether a ring is formed.
5. There is also a very bad situation. Note that not only does the Input-1 and-1 test end, but the input of both numbers is less than 0.

# Include <iostream> # include <cstdio> # include <string. h> # include <math. h ># include <algorithm> using namespace std; const int N = 10005; int a, B, n, mi, mx, sum, flag, v [N], pre [N]; int find (int x) // find the root node of the tree to which x belongs {int r, I, j; r = x; I = x; while (r! = Pre [r]) r = pre [r]; while (pre [I]! = R) {j = pre [I]; pre [I] = r; I = j;} return pre [x];} int main () {n = 0; while (scanf ("% d", & a, & B )! = EOF) {memset (v, 0, sizeof (v); // point involved in the record for (int I = 1; I <= N; I ++) pre [I] = I; if (a <0 & B <0) break; else if (a = 0 & B = 0) // special processing, empty tree {printf ("Case % d is a tree. \ n ", ++ n); continue;} pre [B] = a; v [a] = 1; v [B] = 1; mi = min (, b); mx = max (a, B); flag = 1; while (scanf ("% d", & a, & B )! = EOF) {if (a = 0 & B = 0) break; if (mx <a | mx <B) mx = max (a, B ); if (mi> a | mi> B) mi = min (a, B); v [a] = 1; v [B] = 1; if (flag = 1) {int fx = find (a); int fy = find (B); if (fy! = B) | (fy = fx) // If a ring is formed or the child node already has a parent node, the edge flag is invalid; else if (fy = B & fy! = Fx) // if the ring is not formed and there is no Father's Day, it is legal to add pre [B] = fx ;}} int sum = 0; if (flag = 1) {for (int I = mi; I <= mx; I ++) // determine whether multiple trees exist {if (v [I] = 1) {int fx = find (I); if (fx = I) {sum ++; if (sum> 1) {flag = 0; break ;}}} if (flag = 1) printf ("Case % d is a tree. \ n ", ++ n); else if (flag = 0) printf (" Case % d is not a tree. \ n ", ++ n);} return 0 ;}View Code

 

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.