HDU 1272 small Greek maze (query set + Minimum Spanning Tree + Queue)

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1272

Xiaoxi's maze

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 26818 accepted submission (s): 8273


Problem description the last time gardon's Labyrinth Castle was played for a long time (see Problem B). Now she wants to design a maze for gardon. However, she had different ideas for designing the maze. First, she thought that all the channels should be connected in two directions. That is to say, if one channel connects room A and B, it can be used to go from room A to Room B, or from Room B to room A to improve the difficulty, john hopes that any two rooms can have only one path to communicate with each other (unless they go back ). Xiaoxi now gives you her design drawing to help you determine whether her design drawing conforms to her design idea. For example, in the following example, the first two conditions are met, but the last one has two methods from 5 to 8.
 

 

The input contains multiple groups of data. Each group of data is a list of integer pairs ending with 0, indicating the numbers of the two rooms connected by one channel. The number of the room must be at least 1 and cannot exceed 100000. There is an empty row between each two groups of data.
The entire file ends with two-1 characters.

 

For each group of input data, output only contains one row. If the maze conforms to Xiao Xi's idea, "yes" is output; otherwise, "no" is output ".

 

Sample input6 8 5 3 5 2 6 4 5 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 4 5 5 6 5 2 0 0-1-1

 

Sample outputyesyesno: A simple understanding is: (1) it cannot constitute a circle (2) it is necessary to satisfy all the two conditions on a tree. Here, I used a queue to solve the problem of whether a tree exists. I first saved it to the queue and then obtained it for determination. Note that when the input is 0, the output value is yes. For details, see the code.
 1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 #include <cstring> 5 using namespace std; 6  7 int father[100009]; 8  9 void set()10 {11     for (int i=1; i<100009; i++)12         father[i]=i;13 }14 15 int find(int a)16 {17     while (father[a]!=a)18         a=father[a];19     return a;20 }21 22 int  Union(int x,int y)23 {24     x=find(x);25     y=find(y);26     if (x!=y)27     {28         father[x]=y;29         return 0;30     }31     else32         return 1;33 }34 35 int main ()36 {37     int a,b,k,used[100009];38     while (~scanf("%d%d",&a,&b))39     {40         if (a==0&&b==0)41         {42             printf ("Yes\n");43             continue;44         }45         memset(used,0,sizeof(used));46         queue<int>q;47         q.push(a);48         q.push(b);49         used[a]=1;50         used[b]=1;51 52         int flag=0,cmp=0;53         set();54         if (a==-1&&b==-1)55             break;56         Union(a,b);57         while (scanf("%d%d",&a,&b),a||b)58         {59             if (Union(a,b)==1)60                 flag=1;61             if(used[a]!=1)62             {63                 q.push(a);64                 used[a]=1;65             }66             if (used[b]!=1)67             {68                 q.push(b);69                 used[b]=1;70             }71         }72         while (!q.empty())73         {74             int s=q.front();75             q.pop();76             if (s==father[s])77                 cmp++;78         }79         if (flag==0&&cmp==1)80             printf ("Yes\n");81         else82             printf ("No\n");83     }84     return 0;85 }

 

 

HDU 1272 small Greek maze (query set + Minimum Spanning Tree + Queue)

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.