Hdoj 1512 almost template left-leaning tree

Source: Internet
Author: User

The main problem: there are n monkeys. Each monkey has a strength value, the greater the strength value means that the monkey fights the more severe. If 2 monkeys do not know, they will find the monkeys they know the most powerful out singled out, regardless of winning or losing, singled out the 2 monkeys power value halved, the 2 monkeys are known, do not know well. Now to the M group, if 2 monkeys know each other, output-1, or they each find their own know the most bull of the monkey singled out, to pick up after this dial the Monkey power maximum value.

Left side big root Plus and check

The code is lazy paste ...

1#include <iostream>2#include <fstream>3#include <sstream>4#include <algorithm>5#include <string>6#include <Set>7#include <map>8#include <utility>9#include <queue>Ten#include <stack> One#include <list> A#include <vector> -#include <cstdio> -#include <cstdlib> the#include <cstring> -#include <cmath> -#include <ctime> - using namespacestd; + Const intMAXN =100010; - structNode { +         intL,r,dis,val,par; A } HEAP[MAXN]; at intN, M; - intFind (int&x) { -     returnHeap[x].par = = x? X:heap[x].par =find (Heap[x].par); - } - intMergeintRt1,intrt2) - {   in     if(rt1==0)returnRt2;  -     if(rt2==0)returnRt1;  to     if(heap[rt2].val>heap[rt1].val) Swap (RT1,RT2);  +HEAP[RT1].R =merge (HEAP[RT1].R,RT2); -Heap[heap[rt1].r].par =Rt1; the     if(heap[heap[rt1].l].dis <heap[HEAP[RT2].R].dis) * swap (HEAP[RT1].L, HEAP[RT1].R); $     ElseHeap[rt1].dis = heap[HEAP[RT1].R].dis +1;Panax Notoginseng     returnRt1;  - }   the intPush (intXinty) { +     returnmerge (x, y);  A } the intPop (int&x) { +     intL =HEAP[X].L; -     intR =HEAP[X].R; $Heap[l].par =l; $Heap[r].par =R; -HEAP[X].L = HEAP[X].R = Heap[x].dis =0;  -     returnmerge (L, R);  the } - BOOLScan_d (int&num) {Wuyi     Char inch;BOOLisn=false; the     inch=GetChar (); -     if(inch==eof)return false; Wu      while(inch!='-'&& (inch<'0'||inch>'9'))inch=GetChar (); -     if(inch=='-') {isn=true; num=0;} About     Elsenum=inch-'0'; $      while(inch=getchar (),inch>='0'&&inch<='9'){ -num*=Ten, num+=inch-'0'; -     } -     if(IsN) num=-num; A     return true; + } the intMain () { -      while(Scan_d (N)) { $           for(inti =1; I <= N; ++i) { the Scan_d (heap[i].val); theHEAP[I].L = HEAP[I].R = Heap[i].dis =0; theHeap[i].par =i;  the          } - Scan_d (M); in          intA, B, X, y; the           while(M-- ) { the Scan_d (a); Scan_d (b); Aboutx =find (a); they =find (b); the                 if(x = =y) { thePuts ("-1" );  +}Else { -Heap[x].val/=2; the                     intPX =push (POP (x), x); BayiHeap[y].val/=2; the                     intPY =push (Pop (y), y);  the                      -printf ("%d\n", heap[merge (px, py)].val);  -                 }     the          }  the     } the     return 0; the}
View Code

Hdoj 1512 almost template left-leaning tree

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.