HDU 1272 The maze of little Nozomi

Source: Internet
Author: User

The maze of Xiaoxi

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 34543 Accepted Submission (s): 10573


problem Descriptionthe last time Gardon Maze Castle Little Nozomi played for a long time (see Problem B), now she also want to design a maze let Gardon to go. But she designed the maze of different ideas, first she thought all the channels should be two-way connectivity, that is, if there is a channel connected to room A and B, then can go through it from room A to room B, but also through it from Room B to room A, in order to improve the difficulty, Xiaoxi hope that any two rooms have and only one path can be connected (unless you go back). 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 has two methods of reaching 8 from 5.

 
InputInput contains multiple sets of data, each set of data is a list of integers ending in 0 0, representing the number of two rooms connected by a channel. 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 two-1.
 
Outputfor each set of data entered, the output includes only one row. If the maze conforms to Xiaoxi's idea, then output "Yes", otherwise output "No".
 
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 3  5 6  5 2  0 0-1-1
 
Sample Output
Yesyesno
 
AuthorGardon 
SourceHDU 2006-4 Programming ContestIdeas:first we need to know, under what circumstances to satisfy the idea of little Nozomi, under what circumstances do not satisfy the idea of the small, then we have to think about the input data, when all the end, when this input end, ready to output the results. We are now going to answer each of them: the idea of satisfying little Nozomi must have only one root, and cannot have a ring; when input two-1 is the end of the loop, when the input two 0 is the end of the cycle, the output results, but when the beginning of the input two 0, direct output Yes, do not ask why, This is the loophole of this problem, do not pick this point, this point pits a lot of people! The essence of the problem is to determine the number of root and whether it is a ring!Code:

 #include <stdio.h> #define MAX 100005int pre[max];//represents the parent node int vis[max];//Mark whether the input int flag;//tag conforms to the condition int find (int x)/ /Find root node and compress path {int r;r=x;while (r!=pre[r]) r=pre[r];int i,j;i=x;while (i!=r) {j=pre[i];p re[i]=r;i=j;} return r;} void join (int x,int y)//Connect the number of different root nodes together {int Fx=find (x), int fy=find (y), if (fx!=fy)//If the root node is different, connect two numbers of different root nodes together pre[fx]=fy; else//otherwise the same root node, and then also need to connect its two points, so the description for the loop, not meet the conditions, flag=0; flag=0;} int main () {int a,b;while (scanf ("%d%d", &a,&b)!=eof) {if (a==-1&&b==-1)//When input two-1 o'clock end input break;if (a==0     &&b==0)//When input two 0 o'clock end this time input {printf ("yes\n");//Output Yes, this point compared to the pit, the problem is not explained, to pay special attention!!!      Continue }for (int i=1;i<=100005;i++)//Build 100,005 rooms, there is no road between, that is, establish 10,005 nodes {pre[i]=i;vis[i]=0;//labeled 0}vis[a]=1;vis[b]=1;// Enter which one to mark which, because the input is not continuous, to the back of the judgment there are a few roots when the mark is used here. (Only a few of the root of the input are judged) flag=1;//assumes that the input satisfies the requirement, and when it does not meet the requirements, assigns a value of 0 join (A, b); && (a| | b)//Continue to enter and determine whether it is 0, if two are 0, then end the loop! {vis[a]=1;//tag Input vis[b]=1;join (A, b);} int s=0;for (int i=1;i<100005;i++)//i==pre[i] is a sufficient and necessary condition for I to be the root node! {if (i==pre[i]&&vis[i])//vis[i] cannot be written to the For Loop judgment statement, because if the data is not contiguous, it will not be able to determine whether the following is the root s++;//because the number of inputs is not necessarily continuous, so only in the number of inputs can be judged that there are several root , each number that is not entered is the root if (s>1)//When the condition is not satisfied when it is greater than one root, so flag is assigned a value of 0, jumping out of the loop {Flag=0;break;}} If the IF (flag==1)//flag tag satisfies the requirements of the small Greek: 1 for the match, 0 for the non-compliant printf ("yes\n"), Else printf ("no\n");} return 0;}


Search

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1272 The maze of little Nozomi

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.