Leftist tree (p1456) comparing the template a question

Meaning of the questions: There are monkeys n, m operations, each operation will make two piles of monkeys in the two biggest fights, so after, their own weight in half, then they will become friends

That is, it will belong to the same tree, details: If elected with a bunch of monkeys, outputs -1, then the next operation, do not fight;

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<string.h>
 4 #include<math.h>
 5 using namespace std;
 6 const int maxn=1e5+10;
 7 int val[maxn];
 8 int f[maxn];
 9 int ch[maxn][2];
10 int dis[maxn];
11 int getf(int x)  //标准并查集
12 {
13     if(f[x]==x) returnX;
 14      the else {
 15          F [X] = GETF (F [X]);
 16          return F [X];
 . 17      }
 18 is  }
 . 19  int the Merge ( int X, int Y)
 20 is  {
 21 is      IF (X ||!! Y) return X + Y;   // in the end of;
 22      // ensure minimum back understand the nature of the stack 
23 is      IF (Val [X] < Val [Y]) the swap (X, Y);
 24      // this is probably Chong people of this algorithm used to it, puts it at the right subtree.
25      // then operates to satisfy the following properties to the left of the tree; 
26 is      CH [X] [ . 1] = The Merge (CH [X] [ . 1 ], Y);
 27      F [CH [X] [ . 1 ]] = X;    // disjoint-set operation;
 28      // satisfy left; 
29      IF (DIS [CH [ X] [ 0 ]] <DIS [CH [X] [ . 1 ]]) the swap (CH [X] [ 0 ], CH [X] [ . 1 ]);
 30      // this is the nature of the left of the tree, think you know it is right. 
31 is      DIS [X] = DIS [CH [X] [ . 1 ]] + . 1 ;
 32      return X;
 33 is  }
 34 is  int main ()
 35  {
 36      int n-, m;
 37 [      the while (Scanf ("%d",&n)!=EOF){
38         memset(dis,0,sizeof(dis));
39         memset(ch,0,sizeof(ch));
40         for(int i=1;i<=n;i++){
41             scanf("%d",&val[i]);
42             f[i]=i;
43         }
44         scanf("%d",&m);
45         while(m--){
46             int t1,t2;
47             scanf("%d%d",&t1,&t2);
48             int u=getf(t1);
49             int v=getf(t2);
50             if(u==v){
51                 printf("-1\n");
52                 continue;
53             }
54             val[u]/=2;
55             int root=Merge(ch[u][1],ch[u][0]);
56             ch[u][1] = CH [U] [ 0 ] = DIS [U] = 0 ;
 57 is              int newx = the Merge (the root, U);
 58              Val [V] / = 2 ;
 59              the root = the Merge (CH [V] [ . 1 ], CH [V] [ 0 ]);
 60              CH [V] [ . 1 ] = CH [V] [ 0 ] = DIS [V] = 0 ;
 61 is              int newy, = the Merge (the root, V);
 62 is              the root = the Merge (newx , newy,);   
 63 is              F [newx] = F [newy,] = the root;    // where two points have, in fact, requires only one point,
 64              // because the parent is not only from a point of the root has been halved that node update back.
65              // need to be updated is the root node point; 
66              the printf ( " % D \ n- " , Val [the root]);
 67          }
 68      }
 69      return  0 ;
 70 }

 

Guess you like

Origin www.cnblogs.com/pangbi/p/11729794.html