P2024 [NOI2001] food chain (Los Valley)

  Topic Portal

  Core Solution (disjoint-set):

    The key to reason clearly how these relationships: that is intended to be seen by the problem-solving: Suppose there are the following three types of animals: tiger, food, tiger predators. Xy entered only satisfies these three types (input m times to judge relationship), xy is unknown two separate animal species (i.e. do not know the tiger body, food or predators). Now we need to build and query the relationship between individuals, so we need to use the disjoint-set, but disjoint-set from the maintenance of the relationship between the two to maintain the relationship between the three. And the template disjoint-set still apply, can be directly set, but when initialized by the number of elements n to 3n. The reason is as follows: Previously only need to determine whether it can be set to the same set of n, but here there is a body, food, natural enemies of these three groups, not just from the judge to question whether a group.
    For ease of storage, double the deposit itself, keep food twice, three times the deposit predators; but we need to pay attention to: predators can eat their own food to eat predators.

 

Implementation code:

 

. 1 #include <bits / STDC ++ H.>
 2 #include <CString>
 . 3  the using  namespace STD;
 . 4  
. 5  #define MAX_N 500 005
 . 6  int n-, m, K;
 . 7  int Rank [MAX_N];             // height of the tree 
. 8  int PAR [MAX_N]; 
 . 9  int ANS = 0 ;                     // record the number lies 
10  int T [MAX_N], X-[MAX_N], the Y [MAX_N]; 
 . 11  void the init () {
 12 is      for ( int I = . 1 ; I <= 3n-*; I ++ ) {
 13 is          PAR [I] = I;
 14          Rank [I] = 0 ;
 15      }
 16  }
 . 17  
18 is  int Find ( int X) {         // query tree root 
. 19      IF (PAR [X] == X) {
 20 is          return X;
 21 is      } the else  return PAR [X] = Find (PAR [X]);         // actually directly connected to the parent node in the search process is optimized to the root node 
22 is  }
 23 is  
24  void Unite ( int X, int Y) {
 25      X =Find (X);
 26 is      Y = Find (Y);
 27      
28      IF (X == Y)     return ;
 29      
30      IF (Rank [X] < Rank [Y]) {
 31 is          PAR [X] = Y;
 32      } the else {
 33 is          PAR [Y] = x;
 34 is          IF (Rank [x] == Rank [Y]) Rank [x] ++;         // time as high as two trees, the trees were combined in x height plus 1 X 
35      }
 36  }
 37 [  
38 is  BOOL Same ( int X, int Y) {
 39      return Find (X) ==find(y);
40 } 
41 
42 int main(){
43     cin>>n>>m;
44     for(int i=1;i<=m;i++){
45         cin>>T[i]>>X[i]>>Y[i];
46     }
47     init();
48     
49     for(int i=1;i<=m;i++){
50         int t=T[i],x=X[i],y=Y[i];
51         
52         if(x<1||x>n||y<1||y>n){    //先判断边界(满足条件2)
53 is              ANS ++ ; 
 54 is              Continue ;         // skip the next sentence to see 
55          }
 56 is          
57 is          IF (T == . 1 ) {             // represents xy-grade 
58              IF (Same (X, Y + n-) || Same (X, Y + 2 * n-)) { // if x is x or y is y prey to predators (xy represents two animals of different classes), clearly lies ( 
59                  ANS ++ ;
 60                  Continue ;
 61 is              } the else {                 // the following are from the perspective of tiger (bulk) view 
62 is                  Unite (X, Y);         //The body and the body is of a type, so to merge 
63 is                  Unite (x + n-, y + n-);     // foods and food is a type (i.e., x food is y food) 
64                  Unite (x + 2 * n-, + y 2 * n-);      // predators and natural enemies (i.e., type of x is a natural enemy of predators y) 
65              }
 66          } the else  IF (T == 2 ) {         // represents eaten x y (i.e. y is x when the food relationships) 
67              IF (x == y) {         // if x == y representation is the same animal, but it is not necessary to write a little speed 
68                  ANS ++ ;
 69                  the Continue ;
 70              }
 71              IF(Same (x, y) || Same (x + 2 * n-, y)) {     // represents animals x and y are the same type of animal (body are assumed to run) x or y is a natural enemy (i.e. y eat x), indicates that this is a lie 
72                  ANS ++ ;
 73 is                  Continue ;
 74              } the else {
 75                  Unite (X + n-, Y);             // food and y x same type 
76                  Unite (X + 2 * n-, Y + n-);         / / predators of x to y is the same type of food 
77                  Unite (x, y + 2 * n-);                 // predators x and y of the same type 
78              }
 79          }
 80      }
 81     cout<<ans;
82     return 0;
83 }

 

Guess you like

Origin www.cnblogs.com/xwh-blogs/p/12608176.html