Is it a tree?Crawling in process...
Crawling failed
TIme limit:1000 ms
Memory limit:10000kb64bit Io format:% I64d & % i64u
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.
Each group of data ends at 0 to 0, and the entire program ends at-1 to 1. Each number x y indicates that Y is a subnode of X, there is a directed edge directed by X to Y. The problem is whether the given data constitutes a tree.
Question: First, you must know how to build a tree. The following situations do not match:
1. If there are multiple root nodes, there will be multiple trees.
2. Each son has only one father;
3. You cannot be your father;
4. The two fathers and sons cannot be repeated.
5. It is also the easiest place to be pitted: it cannot be circled and the empty tree is also a tree (it has been pitted for an afternoon ....)
Code:
# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; int countn, flag, flag1, Z [1005], menber, R, father; struct node {int X; int num; // used to record the number of child nodes} No [1005]; int finda (int) // search for the root node and compress the path {int I, j; r = A; while (no [R]. x! = R) {r = No [R]. X;} I = A; while (I! = R) {J = No [I]. x; no [I]. X = r; I = J;} return r;} void Init (int A, int B) {int F1, F2; if (a = B) Flag = 1; // if you are your father, F1 = finda (a); F2 = finda (B); If (F1! = F2) {No [F2]. X = F1; countn ++;} No [B]. num ++; If (no [B]. num> 1) Flag = 1; // when the number of child nodes exceeds 1, the rule is directly excluded} int main () {int A, B, I, y = 1; while (scanf ("% d", & A, & B) {flag = 0; flag1 = 1; menber = 0; countn = 0; memset (Z, 0, sizeof Z); for (I = 0; I <1005; I ++) {No [I]. X = I; no [I]. num = 0;} if (a =-1 & B =-1) break; else if (a = 0 & B = 0) {printf ("case % d is a tree. \ n ", y ++); flag1 = 0;} else {Init (a, B); Z [a] ++; Z [B] ++; while (scanf ("% d", & A, & B) & (A | B) {Init (A, B ); Z [a] ++; Z [B] ++ ;}} for (I = 0; I <1005; I ++) {If (Z [I]) menber ++;} If (flag1) {FATHER = No [R]. x; // determines whether a circle is formed. If (flag = 0 & (menber-countn = 1) & (no [Father]. num = 0) printf ("case % d is a tree. \ n ", y ++); else printf (" case % d is not a tree. \ n ", y ++) ;}} return 0 ;}