Hdu 1272 Little Greek maze (simple and collection)

Source: Internet
Author: User

The maze of XiaoxiTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 31396 Accepted Submission (s): 9726


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

Authorgardon
Sourcehdu 2006-4 Programming Contest


Just learn and check set, see this topic know can use and check set, but found that the light on their own or not .... Look at other people's thinking: 1. Judgment is not a ring. 2. As long as the number of nodes = number of sides +1. Two conditions are met to output "yes". According to this idea, just beginning to find root, if found two root is the same, then directly output no, then break off. Otherwise, the relationship between the number of nodes and the edge will be judged finally.

And then it's wa ....... Go to other people's blogs and find out more about the trees. A look at their own code, found himself in the root of the same time is a direct break off, did not consider more than one tree, once this, will output a number of conclusions.

Code:

#include <stdio.h>
#include <string.h>
#define M 1000005
int father[m],vis[m];
int findroot (int x)
{
int r=x;
while (FATHER[R]!=R)
R=FATHER[R];
return R;
}
int merge (int x,int y)
{
int fx,fy;
Fx=findroot (x); Fy=findroot (y);
if (fx!=fy)
{
if (fx>fy) father[fx]=fy;
else Father[fy]=fx;
}
}
int main ()
{
int I,j,k,a,b,mini=m,maxi=-1,t=1,edge=0,flag;
while (scanf ("%d%d", &a,&b)!=eof)
{
t=1;edge=0;flag=0;
if (a==-1&&b==-1) break;
if (a==0&&b==0) {
printf ("yes\n");
}
memset (vis,0,sizeof (VIS));
for (i=1;i<=1000000;i++)
Father[i]=i;
if (!vis[a]) vis[a]++;
if (!vis[b]) vis[b]++;
while (scanf ("%d%d", &a,&b))
{
if (a==0&&b==0)
{
for (i=1;i<=1000000;i++)
if (vis[i]) edge++; See the number of nodes
if (t!=edge-1)
flag=1;
if (flag==0) printf ("yes\n");
else printf ("no\n");
Break
}
t++; Look at the number of edges
if (!vis[a]) vis[a]++;
if (!vis[b]) vis[b]++;

if (Father[a]!=father[b])
{
Merge (A, b);
}
else {
flag=1; Originally there was a break, and later found that the wrong, changed to a mark, and considered the situation of many trees
}


}
}
return 0;
}

Hdu 1272 Little Greek maze (simple and collection)

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.