Num 21:hdoj: Title 1272: The Maze of Xiaoxi (and find out the problem)

Source: Internet
Author: User


Topic:

The maze of Xiaoxi Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 35368 Accepted Submission (s): 10808


Problem description Last 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.


Input 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 two-1.

Output contains only one row for each set of data that is entered. 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 4 5 3  5 6  5 2  0 0-1-1

Sample Output
Yesyesno
It is also a question about and checking the set, the key is:

1. Determine whether a loop exists in the collection:

Implementation code:

int join (int x,int y) {int fx=find (x), int fy=find (y), if (fx!=fy) {Per[fx]=fy;return 1;} else return 0;}
define the Join function as type int:

If there is a loop point (that is, for different data, there is the same root node), the return is 0;


2.3. The number of root nodes in the collection is judged by the tag of the node used:

Implementation code:

while (a| | b) {if (A>max) max=a;          if (B>max) max=b;          if (a<min) min=a;          if (b<min) min=b;          Mark[a]=1;mark[b]=1;          if (join (b) ==0) flag=0;        scanf ("%d%d", &a,&b);} if (flag==0) printf ("no\n"), else{count=0;for (int i=min;i<=max;i++) if (mark[i]&&per[i]==i) count++;if ( count==1) printf ("yes\n"), Else printf ("no\n");}

Mark the used number by defining a mark array [labeled 1];

Two Min,max, find the beginning and end of a number;

The count records the number of root nodes, judging whether mark (or not) and whether it is the root node;


In addition, one of the reasons why this problem is called a problem is:

when you enter only 0 0, you also want to output yes;


AC Code:

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include < Malloc.h>int per[100010];int mark[100010];void Init () {for (int i=1;i<=100010;i++) per[i]=i;} int find (int x) {int r=x;while (r!=per[r]) r=per[r];int p,temp;p=x;while (p!=r) {temp=per[p];p er[p]=r;p=temp;} return r;} int join (int x,int y) {int fx=find (x), int fy=find (y), if (fx!=fy) {Per[fx]=fy;return 1;} else return 0;} int main () {int min,max,count,a,b,flag=1;while (scanf ("%d%d", &a,&b)!=eof) {if (a==-1&&b==-1) break;if (a==0&&b==0) {printf ("yes\n"); continue;} Init (); Min=99999999;max=-1;while (a| |              b) {if (A>max) max=a;              if (B>max) max=b;              if (a<min) min=a;              if (b<min) min=b;          Mark[a]=1;mark[b]=1;        if (join (b) ==0) flag=0; scanf ("%d%d", &a,&b);} if (flag==0) printf ("no\n"), else{count=0;for (int i=min;i<=max;i++) if (mark[i]&&per[i]==i) count++;if ( count==1) printf ("yes\n"), Else printf ("no\n");}} RetUrn 0;} 



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

Num 21:hdoj: Title 1272: The Maze of Xiaoxi (and find out the problem)

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.