(╭ ̄3 ̄) ╭ The maze of the Little Nozomi II

Source: Internet
Author: User

(╭ ̄3 ̄) ╭ The Maze iitimelimit:2000/1000 MS (java/others) menorylimit:65536/32768 K (java/others) 64-bit integer IO format:%i64dProblem Description
The last time Gardon Maze Castle little hope to play for a long time, now she also want to design a maze let Gardon to walk. But her idea of designing the maze was different, and first she thought all the passages were supposed to be Unidirectional Connectivity, that is, if there is a channel connecting rooms A and B, then it can only go from room A to room B, in order to improve the difficulty, Xiaoxi hope that any two rooms have and only one path can be connected (unless go backwards), and must be tree-shaped structure, there can only be one root node (no point pointing at it!!!) )。 Xiao-Nozomi now gives you her design to help you determine whether her design is in line with her design ideas. For example, the first two are eligible, but the last one does not fit the desired tree structure.



for each map, it is necessary to meet the above requirements of the tree structure, such a plan is qualified. When only 0 0 is entered, the tree is judgedInput inputs contain multiple sets of data, each of which is a list of integer pairs ending in 0 0, representing the number of two rooms to which a channel is connected. The number of the room is at least 1 and no more than 100000. There is a blank line between each of the two sets of data. The entire file ends with a two negative number. Output contains only one row for each set of data that is entered. If the maze conforms to the idea of Xiaoxi, then the output "case k is a tree.", otherwise the output is a tree. K represents the first group of cases. (as starting from group 1th) sampleinput
6 8 5 3 5 2 6 45 6 0 0
8 1 7 3 6 2 8 9 7 57 4 7 8 7 6 0 0
3 8 6 8 6 45 3 5 6 5 2 0 0
-1-1
Sampleoutput
Case 1 is a tree. Case 2 is a tree. Case 3 was not a tree.

Solution One: (Only in the maze of the small Greek added to the degree of conditions can be)
And Xiao-Xi's maze, basically the same, but the composition of the diagram into a map, but also need to determine whether the graph is able to form a tree
Enter a, B, which indicates that point a points to point B. until input 0 0 starts judging, the input All PointsCan forming a forward connected graphAnd No loopand is Tree
1, according to the knowledge of graph theory is easy to know, all points (for example, there are N pointsM-Bar non-repeating sideTo form a connected graph, only the m=n-1 can be judged.
2, use and check the knowledge of the set, judge whether there is a loop, just to determine whether the ancestor of two points is the same.
3, need to pay more attention to the point is, also need to determine the direction of the connected graph can form a tree, not so the need to have to connect to the graph is a tree.
   structure of the tree of the topic requirementis: The direction of the arrow is a child node, so it is necessary to determine the degree of penetration.
For example, enter a B, indicating that a points to B, both A is the Father node of B, B is the child node of a, as long as the entry status of B is counted.
And the definition of a treeIs have and have only one root nodeThe root node of the tree has an entry level of 0 and the remaining nodes are 1
For example, input: 1 2 3 2 0 0, the figure represented is a graph, but he does not have The structure of the tree required by the topic
So, the tree that can make up the subject needs to be judged by the following conditions:
The penetration of all points and (ind_num==n-1) or (ind_num==m),
PS:Enter a negative number to indicate the end.
1#include <iostream>2#include <stdio.h>3 using namespacestd;4 #defineMax 1001005 intId[max];6 intInd[max];7 intOut_num;8 intPio[max];9 intSign ;Ten intP_num; One voidCread (intN) A { -      for(intI=0; i<=n;i++) {id[i]=i; pio[i]=1; ind[i]=1;} - } the intFind (intx) - { -     intTmd=x,tmp; -      while(TMD!=ID[TMD]) Tmd=ID[TMD]; +      while(x!=TMD) -     { +tmp=Id[x]; Aid[x]=TMD; atx=TMP; -     } -     returnx; - } - voidUpdate (intAintb) - { in     if(Pio[a]) {p_num++; pio[a]=0;} -     if(Pio[b]) {p_num++; pio[b]=0;} to     if(Ind[b]) {out_num++;ind[b]=0;} +     intA=Find (a); -     intb=Find (b); the     if(a!=B) *     { $id[a]=B;Panax Notoginsengsign++; -     } the     Elsesign=Max; + } A intMain () the { +     intt,n,m,i,j,a,b,t=1; -Cread (Max); sign=0; p_num=0; out_num=0; $      while(SCANF ("%d%d", &a,&b)! =EOF) $     { -         if(a<0&&b<0) Break; -         if(a==0&&b==0) the         { -            //printf ("%d%d%d\n", sign,out_num,p_num);Wuyi             if((sign==p_num-1|| p_num==0) &&out_num==Sign ) theprintf"Case %d is a tree.\n", t++); -             Else Wuprintf"Case%d was not a tree.\n", t++); -Cread (Max); sign=0; p_num=0; out_num=0; About             Continue; $         } - Update (A, b); -     } -     return 0; A}
View Code
Positive solution: (only according to the definition of the tree, you can determine the degree of penetration ...) )
1#include <stdio.h>2#include <algorithm>3#include <string.h>4 using namespacestd;5 intd[100005];6 BOOLbo[100005];7 intbianshu=0;8 voidinit () {9memset (D,0,sizeof(d));TenMemset (Bo,0,sizeof(bo)); Onebianshu=0; A } - intMain () { -     intx,y,cas=1; the      while(~SCANF ("%d%d",&x,&y)) -     { -         if(x==-1&&y==-1){ -              Break; +         } -         if(x==0&&y==0){ +             intbobo=0; A             inti; at              for(i=1; i<=100000; i++){ -                 if(bo[i]&&d[i]==0){ -                     if(bobo==0) bobo=1; -                     Else  Break; -}Else -                 if(bo[i]&&d[i]!=1){ in                      Break; -                 } to             } +             if(i<=100000) printf ("Case%d was not a tree.\n", cas++); -             Elseprintf"Case %d is a tree.\n", cas++); the init (); *             Continue; $         }Panax Notoginsengd[y]++; -bo[x]=true; thebo[y]=true; +bianshu++; A     } the     return 0; +}
View Code


(╭ ̄3 ̄) ╭ The maze of the Little Nozomi II

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.