HDU-1272-the labyrinth of xiaoxi [parallel query set] [connected graph]

Source: Internet
Author: User

 

Xiaoxi's maze

 

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 19647 Accepted Submission (s): 6009

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 45 6  0 08 1  7 3  6 2  8 9  7 57 4  7 8  7 6  0 03 8  6 8  6 45 3  5 6  5 2  0 0-1 -1
Sample Output
YesYesNo
 

Code:

At the beginning, the array is allocated 100 k, which is small... no problem ~

# Include "stdio. h "# include" string. h "int set [100100]; int vis [100100]; int flag; int search (int n) {int I = n; while (set [I]! = I) // find the root node of n, and n must be paid to I because the following statement uses n and cannot change it. I = set [I]; int j = n; while (j! = I) // when n does not point to the root node just found, point all n's fathers to I {// optimize int temp = set [j]; set [j] = I; j = temp;} return I;} void update (int a, int B) {if (search (a) = search (B )) // there is no common root node, indicating that the connection flag is not 1; else set [search (a)] = search (B);} void connect () // determine whether to connect {int I, count = 0; for (I = 1; I <100100; I ++) {if (! Vis [I] & set [I] = I) {count ++; // printf ("count: % d \ n", count );} if (count> 1) {flag = 1; break;} // if the root node number is greater than 1, // as the graph is required, if you point to your own one, there is a Ring} int main () {int a, B, I; while (scanf ("% d", &, & B),! =-1 & B! =-1) {memset (vis, true, sizeof (vis); if (a = 0 & B = 0) {printf ("Yes \ n "); continue;} for (I = 0; I <100100; I ++) // initialize the parent node set [I] = I; flag = 0; update (a, B ); vis [a] = vis [B] = false; // The vertex of the input edge is set to while (scanf ("% d", &, & B), a & B) {update (a, B); vis [a] = vis [B] = false;} connect (); if (flag = 0) printf ("Yes \ n"); else printf ("No \ n ");}}

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.