HDU 1272 The maze of the Little Nozomi (and the reason for the collection/explosion stack)

Source: Internet
Author: User

Little Nozomi's Maze Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 33194 Accepted Submission (s): 10214


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 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
Author Gardon
Source HDU 2006-4 Programming Contest

The first time this problem uses the number of vertices = number of sides +1, although also a, but this method should be incorrect.

Because if it is

1 2

3 4

3 5

4 5

0 0

Definitely not in line with ... So we use and check the set to determine whether the ring. But then there was another mistake. That's the stack ((Stack_overflow))

The reason for the explosion is to find the root in the search function, the compression path there,, recursive so many definitely burst stack, so still do not compress the path to honestly find it

Ideas: And look for the set to determine whether the formation of the ring, if the formation of a ring, must not be established. If the ring is not formed, then determine whether the number of vertices equals the number of sides +1 on the line,

I find the number of vertices also use the Set container, if you do not understand the set container to see this article STL collation

There is also a code for the wrong method that can be a.

Attach and check the Set method first:

#include <stdio.h> #include <set> using namespace std;
const int n=100000+5;
BOOL circle;//judge whether to form a ring int fa[n];
    /*int find (int x)//burst stack is here {if (fa[x]!=x) Fa[x]=find (fa[x]);
return fa[x];
	}*/int find (int x) {while (fa[x]!=x) x=fa[x];
return fa[x];
    } void Uni (int x,int y) {int xx=find (x);
    int Yy=find (y);
    if (xx!=yy) fa[xx]=yy;
else circle=true; } void Init () {for (int i=1;i<n;i++) fa[i]=i;} int main () {set<int>s;//As for this set container can be used to find vertices, the number repeated in this container
        Computes the Edge while (scanf ("%d%d", &a,&b)!=eof) {init () a,b,sum;//sum only once int
        Sum=1,circle=false;
			if (a==0&&b==0)//This is also a place to compare pits {printf ("yes\n");
    	Continue
        } if (a==-1&&b==-1) break;
        S.insert (a);
        S.insert (b);
        if (!circle) uni (a, b);
            while (1) {scanf ("%d%d", &a,&b);
            if (a==0&&b==0) break; S.insert(a), S.insert (b);
            if (!circle) uni (a, b);
        sum++;
        } if (!circle&&s.size () ==sum+1) printf ("yes\n");
        else printf ("no\n");
    S.clear ();
} return 0; }

The wrong way can be a:

#include <stdio.h>
#include <set>
using namespace std;
int main ()
{
	set<int>s;
	int a,b,sum;
	while (scanf ("%d%d", &a,&b)!=eof)
	{
		sum=1;
		if (a==0&&b==0)
		printf ("yes\n");
		if (a==-1&&b==-1) break
		;		
		S.insert (a);
		S.insert (b);
		while (1)
		{
			scanf ("%d%d", &a,&b);
			if (a==0&&b==0) break
			;
			S.insert (a);
			S.insert (b);
			sum++;
		}
		if (S.size () ==sum+1)
		printf ("yes\n");
		else
		printf ("no\n");
		S.clear ();
	}
	return 0;
}


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.