Hdu 1272 small Greek maze (and query set + Minimum Spanning Tree)

Source: Internet
Author: User

Xiaoxi's maze
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Problem Description
The last time Gardon's Labyrinth Castle was played for a long time (see Problem B), she now wants to design a maze for Gardon to come. 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.


 

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

 

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

 

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
Question Analysis: Determine whether the given graph is connected and whether there is a ring. If there is No ring and the connectivity is Yes, otherwise there is No.
And query the application of the Set. Note that if no vertex exists, Yes is also output.

# Include <stdio. h> # include <string. h> # define N 100000 + 10int father [N], num [N]; void initial ()/* initialize */{for (int I = 1; I <N; I ++) {num [I] = 0; father [I] = I ;}} int find (int x)/* find father's Day, find the root node */{if (x = father [x]) return x; else return find (father [x]);} void merge (int a, int B) /* merge two sets */{int p = find (a); int q = find (B); father [p] = q;} int main () {int I, j, x, y, cnt1, cnt2, cnt; while (~ Scanf ("% d", & x, & y) & x> = 0) {initial (); cnt1 = 0; /* How many sides are recorded */cnt2 = 0;/* How Many root nodes are recorded */cnt = 0;/* how many points are recorded */while (x! = 0 | y! = 0) {cnt1 ++; num [x] = 1; num [y] = 1; merge (x, y); scanf ("% d ", & x, & y) ;}for (I = 1; I <N; I ++) /* Find the number of vertices and the number of root nodes */{if (num [I] = 1) cnt ++; if (father [I] = I & num [I] = 1) cnt2 ++;/* Number of root nodes */} if (cnt = 0) {printf ("Yes \ n"); continue;} if (cnt2 = 1 & cnt1 = cnt-1) printf ("Yes \ n "); else printf ("No \ n");} 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.