hdu1272 Xiaoxi's Labyrinth (and check set + judgment ring)

Topic: Given two numbers, there is a path between the two numbers, with 0,0 as the end of a set of data, and -1, -1 as a whole, let you judge whether every two points There is only one path.

Idea: Every two points should be connected (that is, there is a relationship). From this, think of and check the set, and use the search to find the ancestor (root node) to merge these given points, and to satisfy only one path, Then no loops can appear in this set. (Once a loop appears, it means that there are multiple paths between two points. You can draw your own). You can use a flag to determine whether a loop occurs. When looking for an ancestor (root node), two points have the same ancestor (root node)).

There is also a pit in this question. If there are two or more sets, there must be two points that are not connected. At this time, the idea is not satisfied. You can use an array vis to mark those points that have appeared, and then see if All the points that have appeared have only one ancestor.

1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4  using  namespace std;
 5  const  int maxn = 100006 ;
 6  bool flag;
 7  int f [maxn];
 8  int vis [maxn];
 9  int find ( int v) // And find the root node 
10  {
 11      if (f [v] == v)
 12      return v;
 13      else 
14      return find (f [v]);
 15 }
 16  void merge ( int u, int v) // Merge 
17  {
 18      int t1 = find (u);
 19      int t2 = find (v);
 20      if (t1! = T2) // Root node of two points Different means that the two points belong to different sets 
21      {
 22          f [t2] = t1;
 23      }
 24      else // If the two points are already in the same set (the same root node), it means that there is already a way to connect the two points The dots are connected, and then there is no match for the meaning of the question 
25      {
 26          flag = false ;
 27      }
 28  }
 29 int main ()
 30  {
 31      int x, y;
 32      while (~ scanf ( " % d% d " , & x, & y)) // The title requires multiple sets of input. Enter two numbers first to correspond to the following special judgment 
33      {
 34          if (x == 0 && y == 0 ) // Special judgment for this question, if only 0,0 is entered, it is also in line with the requirements, try it out ... 
35          {
 36              printf ( " Yes \ n " );
 37              continue ; // Need to continue to enter the next set of data 
38          }
 39          for ( int i = 0; i <= maxn; i ++) // Each time you input a new set of data, you must initialize 
40          {
 41              f [i] = i; // And check the initialization 
42              vis [i] = 0 ; // The mark appears Passed point 
43          }
 44          if (x ==- 1 && y ==- 1 ) // Enter -1, -1 ends 
45          break ;
 46          merge (x, y); // Merge two points into a set 
47          flag = true ; // This is a sign to judge whether there is a loop 
48          vis [x] = 1 ;
 49          vis [y] =1 ; // Mark the first two points first 
while 50          while (scanf ( " % d% d " , & x, & y), x, y)
 51          {
 52              merge (x, y); // Merge 
53              vis [ x] = 1 ; // Mark 
54              vis [y] = 1 ;
 55          }
 56          if (! flag)
 57          {
 58              printf ( " No \ n " ); // If there is a loop, it does not meet the requirements 
59              continue ;// Continue to the next set of data 
60          }
 61          else // Judge the other conditions without a loop 
62          {
 63              int cnt = 0 ; // Judge how many sets 
64              for ( int i = 0 ; i <= maxn; i ++ )
 65              {
 66                  if (vis [i] && f [i] == i) // There are a few root nodes in the points that have been (given) there are several sets of their own 
67                  cnt ++ ;
 68              }
 69              if (cnt == 1 )
 70              {
 71                 printf ( " Yes \ n " ); // When there is only one set, it meets the intent 
72              }
 73              else 
74              {
 75                  printf ( " No \ n " ); // Otherwise it does not match 
76              }
 77          }
 78      }
 79 }

 

Guess you like

Origin www.cnblogs.com/theshorekind/p/12675419.html