HDU 1272 Little Nozomi's Maze (and check)

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): 40737 Accepted Submission (s): 12539


Problem Description
The 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.



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


Output
For 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 4
5 6 0 0

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

3 8 6 8 6 4
5 3 5 6 5 2 0 0

-1-1


Sample Output
Yes
Yes

No


The problem: The two characteristics of the study and the collection

1: Determine whether it belongs to the same set (can be judged if there is a ring (if the two belong to a set then the Union will have a ring exists)).

2: Determine if there is only one tree (the sum of the vertices in the same tree equals the number of sides plus one).

Use the book "" Array to determine if a point has occurred, and then compare it with a point and edge.


Code:

HDU 1272 (maze of Xiaoxi) #include <algorithm> #include <cstdio> #include <cstring> #include <string># Include <iostream>using namespace Std;const int maxx=100005;int par[maxx];int book[maxx];///record Point///Find root node int find (    int x) {if (par[x]==x) return x; else return Par[x]=find (Par[x]);}    Merge collection int unite (int x,int y) {int fx=find (x);    int Fy=find (y);    if (Fx==fy) return 0; else par[y]=x;}    int main () {int x, y;            while (~SCANF ("%d%d", &x,&y)) {if (!x&&!y)///Special data (when x==0&&y==0) output Yes {            printf ("yes\n");        Continue            } if (x==-1&&y==-1) break;        Initializes the root for (int i=1;i<=maxx;i++) par[i]=i;        memset (book,0,sizeof (book));        Book[x]=book[y]=1;        int n=1;///the number of points int flag=1;             while (~SCANF ("%d%d", &x,&y)} {if (x==0&&y==0) break; if (book[x]==0) {n++;             Book[x]=1;                 } if (book[y]==0) {n++;             Book[y]=1;      if (Unite (x, y))////If the two are not in the same collection, then merge (Edge) {n--;        The number of sides of the same tree is different from that of the point 1} else flag=0;        if (flag&&n==1)///If there is only one tree and no ring printf ("yes\n");    else printf ("no\n"); } return 0;}


HDU 1272 Little Nozomi's Maze (and check)

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.