HDU 1272: the labyrinth of xiaoxi (query set)

Source: Internet
Author: User

Xiaoxi's maze


Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 25010 accepted submission (s): 7683


Problem description the last time gardon's Labyrinth Castle was played for a long time (see Problem B). Now she wants to design a maze for gardon. 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.

 
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.
 
For each group of input data, 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

This is also a question for querying and collecting.

Solution: the question is to find a graph to determine whether a graph is connected to a non-circular environment.

1. Judge as a ring: During the reading process, when merging the set, if the two currently read
The element belongs to the same set, so it must be no.

2. Determine Connectivity: you only need to determine the number of root nodes is 1.

# Include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # include <vector> # include <queue> # include <sstream> # include <cmath> using namespace STD; # define F1 (I, n) for (INT I = 0; I <n; I ++) # define F2 (I, n) for (INT I = 1; I <= N; I ++) # define F3 (I, n) for (INT I = N; I> = 1; I --) # define F4 (I, n) for (INT I = 1; I <n; I ++) # define M 100500int f [m]; int flag; int sum; int find (INT X) // query the find {return f [x] = x? X: F [x] = find (F [x]);} void make (int A, int B) {int x = find (); int y = find (B); If (x = y) // if it is the same set, that is, to generate a ring, flag = 1; else f [y] = x;} int main () {int A, B; while (1) {memset (F, 0, sizeof (f); flag = 0; sum = 0; while (scanf ("% d", & A, & B) & A & B) {if (a =-1 & B =-1) return 0; If (F [a] = 0) f [a] =; if (F [B] = 0) f [B] = B; make (a, B);} F4 (I, m) if (F [I] = I) sum ++; // printf ("% d ^ % d", sum, flag ); if (sum> 1 | flag = 1) // No printf ("NO \ n") if a ring or root disconnection node is greater than 1 "); else printf ("Yes \ n");} return 0 ;}

Note: When the input data set is only 0, the condition is still met, that is, "Yes" should be output"








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.