Hdoj 1272 problem-solving Report-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): 11043 accepted submission (s): 3261Problem 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 input6 8 5 3 5 2 6 4 5 6 0 0 8 1 7 3 6 2 8 9 7 5 7 7 8 6 0 0 3 8 6 8 6 4 5 3 5 6 5 2 0 0-1-1 sample outputyes Yes No authorgardon sourcehdu 2006-4 Programming Contest recommendlxj
1 // The stack cannot be recursively called when the compression path is created.
2 // when 0 is input, yes is output.
3
4 # include <cstdio>
5 # define N 100100 + 10
6
7 int father [N];
8 int number [N];
9
10 void ini (void)
11 {
12 for (int I = 1; I <N; I ++)
13 {
14 number [I] = 0;
15 father [I] = I;
16}
17}
18
19 int Find_Set (int)
20 {
21 int B =;
22 while (father [B]! = B)
23 B = father [B];
24 father [a] = B;
25 return father [a];
26}
27
28 void Union (int a, int B)
29 {
30 a = Find_Set ();
31 B = Find_Set (B );
32 father [a] = B;
33}
34
35 int main ()
36 {
37 // freopen ("input.txt", "r", stdin );
38 int X, Y, num, Max, I, num2;
39 while (scanf ("% d", & X, & Y) = 2 & x> = 0)
40 {
41 num = 0;
42 max = 0;
43 num2 = 0;
44 INI ();
45 while (X! = 0 | y! = 0)
46 {
47 num ++;
48 number [x] = 1;
49 number [y] = 1;
50 Union (x, y );
51 scanf ("% d", & X, & Y );
52}
53 For (I = 1; I <n; I ++)
54 {
55 if (number [I] = 1)
56 Max ++;
57 if (father [I] = I & number [I] = 1)
58 num2 ++;
59}
60 // printf ("num2 = % d, num = % d, max = % d \ n", num2, num, max );
61 if (max = 0)
62 {
63 puts ("Yes ");
64 continue;
65}
66 if (num2 = 1 & num = max-1)
67 puts ("Yes ");
68 else
69 puts ("No ");
70}
71 return 0;
72}

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.