Hangzhou Electric 1272 POJ 1308 Little Greek Maze

Source: Internet
Author: User

This problem is I learned and check set after the third problem, teach our sister said this is and check set the basic problem, so it is necessary to firmly grasp.

Here is my experience of doing this problem, give you some suggestions! Of course, my advice is not the best, but also ask the great God to point out my mistakes, I also good to correct.

1. Overview of topics

This question is Hangzhou electric 1272,poj 1308 If you write the code, you can try it.

Little Nozomi's Maze

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 28572 Accepted Submission (s): 8818

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

Yesyesno

2. Topic Understanding

The meaning of the topic should not be difficult to understand, is to give you a lot of pairs of numbers, representative of the number of related rooms, is actually a tree of abstraction, you can think of this as a tree.

Then, you think about the conditions that form the tree.

1. There is a root (root), and the root is not in the degree (no father).

2. Cannot form a ring.

3.N nodes form a tree with n-1 edges.

And then, compared to the topic, it's not just a set of test data that you have to judge is a tree. (There is a pit point, the node of this tree is connected in two directions)

3. Analysis Objectives

1. Input/Output implementation

The input of this question is a little bit special, the first topic is to end with two-1. And each group of test data is ended with two 0.

Output is simple, can output yes, otherwise output No.

2. Implementing the Code

int A, B; Defining the Receive data variable

while (~SCANF ("%d%d", &a, &b) &&! ( a<0&&b<0))

{

int flag=1;

if (a==b)

{

if (a) flag=0; This is a pit point, I have been pit many times. 0 0 is a tree, while the same as 1 1 is not a tree

}

Else

{

int k=1, max=0;

while (a| | b)//Loop input data and save to mark Array

{

Mark[k][0]=a;

Mark[k++][1]=b;

if (max<a) max=a;

if (max<b) max=b;

scanf ("%d%d", &a, &b);

}

_make (max); Initialize the function so that each node points to its own

for (int i=1; i<k; i++)

if (_union (mark[i][0],mark[i][1]))//merge function, make two nodes merge

{

flag=0;

Break

}

if (flag)//below is check to see if it's a tree or it's a deep forest.

{

int Forefather=_find (mark[1][0]);

for (int i=1; i<k; i++)

if (_find (mark[i][0))!=forefather| | _find (mark[i][1])!=forefather)

{

flag=0;

break;

}

}

}

printf ("%s\n", flag?) Yes ":" No "); Sample output

3. Function Introduction

void _make (int max) This function is an initialization function that is intended to have each node point to himself. This function has a formal parameter max he is actually a small optimization. The general situation is initialized from 1 to 100000, the time complexity is O (n), but you actually only need to initialize to the maximum input data on the line, the latter is not necessary.

int _find (int x) This function is a lookup function that is intended to find ancestors. Originally this function can compress the path. But I tested on the anti-power, the compression path after the time has become more, counterproductive so still do not compress the path. The return value of this function is ancestor. If you are not your own ancestor, this function will always be recursive, so sometimes you have to consider whether or not to explode the stack.

int _union (int a, int b) This function is a merge function to link two nodes together. There are two formal parameters, I believe the reader will know the meaning of these two parameters. This function is the most important, use it to call the _find () function, complete the merge work, while judging there is no ring formation.

4. Finally, the pit point analysis of the problem

The pit point of this problem said not much also more, accidentally fell into the pit.

1. That is the 0 0, 1 1 and other special rebel, posture is not possible, fell into this hole.

2. Pop stack, this is not notice, the code is written down will explode stack. Yes, do not compress the path.

3. Determine if there is no ring formation.

4. Judge whether there is a deep forest formation.

I've found so much ...

4. All code

#include <stdio.h>

#define MAX 100001

int Father[max], mark[max][2];

void _make (int max)

{

for (int i=1; i<=max; i++)

Father[i]=i;

return;

}

int _find (int x)

{

if (X!=father[x])

X=_find (Father[x]);

return x;

}

int _union (int a, int b)

{

A=_find (a);

B=_find (b);

if (a==b) return 1;

Father[a]=b;

return 0;

}

int main ()

{

int A, B; //define receive data variables

while (~SCANF ("%d%d", &a, &b) &&! ( a<0&&b<0))

{

int flag=1;

if (a==b)

{

if (a) flag=0; //This is a pit point and I've been in the pit a lot of times. 0 0 is a tree, while the same as 1 1 is not a tree

}

Else

{

int k=1, max=0;

while (a| | b) //Loop input data and save to mark Array

{

Mark[k][0]=a;

Mark[k++][1]=b;

if (max<a) max=a;

if (max<b) max=b;

scanf ("%d%d", &a, &b);

}

_make (max); //Initialize functions so that each node points to its own

for (int i=1; i<k; i++)

if (_union (mark[i][0],mark[i][1])) //merge function, make two nodes merge

{

flag=0;

Break

}

if (flag) //Below is check to see if it's a tree or it's a deep forest.

{

int Forefather=_find (mark[1][0]);

for (int i=1; i<k; i++)

if (_find (mark[i][0))!=forefather| | _find (mark[i][1])!=forefather)

{

flag=0;

break;

}

}

}

printf ("%s\n", flag?) Yes ":" No "); //Sample Output

}

return 0;

}

Finally to everyone left a link, is I do word, thank you!

I will stay in my blog, interested to pay attention to.

Hangzhou Electric 1272 POJ 1308 Little Greek Maze

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.