Summer Camp (2) The first bomb-----is It A Tree? (Poj308)

Source: Internet
Author: User

A-is It A Tree?

crawling in process ... crawling failed time limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld &%llu

Description

A tree is a well-known data structure this is either empty (null, void, nothing) or was a set of one or more nodes Connecte d 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 except the root have exactly one edge pointing to it.
There is a unique sequence of directed edges from the root to each node.
For example, consider the illustrations below, in which nodes is represented by circles and edges is represented by line S with arrowheads. The first of these is trees, and the last was not.

In this problem you'll be given several descriptions of collections of nodes connected by directed edges. For each of these, determine if the collection satisfies the definition of a tree or not.

Input

The input would consist of a sequence of descriptions (test cases) followed by a pair of negative integers. Each test case would consist of a sequence of edge descriptions followed by a pair of zeroes each edge description would con Sist 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 is always greater than zero.

Output

The For all test case display the line ' case K is a tree. ' or the line ' case K ' is not a tree. ', where k corresponds to the Te St Case number (they is sequentially numbered starting with 1).

Sample Input

6 8  5 3 5 2 6 6 0 1 7 3  6 2  8 9  7  4 7 8 7 6 0 8 6 8 6 4 5 3  5 6  5 2  0 0-1-1

Sample Output

Case 1 is a tree. Case 2 is a tree. Case 3 was not a tree.

Problem analysis: The question asks whether is the tree, first clear is the tree condition, the tree is one kind of undirected simple connected graph
1. The absence of loops and loops (2,1) or (1,5) (1,5) is not possible.
2. Points are connected to points, and note that empty trees (that is, a tree without a point) are also trees.

1#include"Cstdio"2 structnode3 {4     inti;5     intJ;6 };7Node a[ +],b[ +];8 intNumber,time=0;9 intNbegin ()Ten { One    intI=0; Atime++; -     while(SCANF ("%d%d", &AMP;A[I].I,&AMP;A[I].J) && (a[i].i! =-1|| A[I].J! =-1)) -    { theNumber =i; -        if(A[i].i | |A[I].J) -i++; -        Else +            return 1; -    } +    return 0; A } at voidtree () - { -    intnow,next,q; - node y; -    if(A[NUMBER].I = =0&& A[NUMBER].J = =0&& Number = =0) -     { inprintf ("Case %d is a tree.\n", time); -             return; to     } +      for(intI=0; i<number;i++) -     { the           for(intk=0; k<number;k++) *         { $          if(A[I].J = = A[K].J && I! =k)Panax Notoginseng             { -printf"Case%d was not a tree.\n", time); the                 return; +             } A          if(a[i].i = = A[K].J && A[I].J = =a[k].i) the              { +printf"Case%d was not a tree.\n", time); -                 return; $              } $         } -     } -     for(intI=0; i<number;i++) the    { -now=0;Wuyinext=0; theq=0; -        for(intt=0; t<number;t++) Wu         if(A[T].I = =a[i].i) -         { Aboutb[next++] =A[t]; $q++; -         } -        while(Next! =Now ) -             { Ay =B[now]; +now++; the                for(intj=0; j<number;j++) -                 if(Y.J = = A[j].i && y.i! =A[J].J) $                 { theb[next++] =A[j]; theq++; the                 } the             } -         if(q = =Number ) in         { theprintf ("Case %d is a tree.\n", time); the                 return; About         } the     } theprintf"Case%d was not a tree.\n", time); the } + intMain () - { the      while(Nbegin ())Bayi tree (); the     return 0; the}
View Code













Summer Camp (2) The first bomb-----is It A Tree? (Poj308)

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.