Zoj 2334 Monkey king/left bias tree + and check set

Source: Internet
Author: User

Original title Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1389

Roughly test instructions: n only monkeys that don't know each other (each monkey has a fighting value)

Two of monkeys do not know the conflict between the two, two monkeys will be asked each of the strongest they know

the monkeys were dueling. after the duel, the two groups of monkeys knew each other.

The duel of the two monkeys combat effectiveness halved ... There are m groups asking

Enter a B to indicate that monkeys A and B are in conflict, and if a, a, is part of the same set output-1

Otherwise the strongest combat value of this group of Monkeys (merged) after the output duel ...

Specific ideas: Use and check set to determine whether it belongs to the same set, with left-leaning tree to maintain a group of monkeys fighting value.

Added garbage collection, otherwise easily burst memory ...

Specific as follows:

1#include <cstdio>2#include <cstdlib>3#include <iostream>4#include <algorithm>5 Const intMax_n =100050;6 structunionfind{7     intPar[max_n], rank[max_n];8InlinevoidInitintN) {9          for(inti =1; I <= N; i++){TenPar[i] =i; OneRank[i] =0; A         } -     } -InlineintFindintx) { the          while(X! =Par[x]) { -x = par[x] =Par[par[x]]; -         } -         returnx; +     } -InlinevoidUniteintXinty) { +x = Find (x), y =find (y); A         if(x = = y)return; at         if(Rank[x] <Rank[y]) { -PAR[X] =y; -}Else { -Par[y] =x; -             if(Rank[x] = = Rank[y]) rank[x]++; -         } in     } - }; to structnode{ +     intV, NPL; -Node *ch[2]; theInlinevoid Set(int_v =0,int_NPL =-1, Node *p =NULL) { *v = _v, NPL =_NPL; $ch[0] = ch[1] =p;Panax Notoginseng     } -Inlinevoidpush_up () { theNPL = ch[1]-&GT;NPL +1; +     } A }; the structleftisttree{ +     intN, top; - Unionfind Rec; $Node *tail, *NULL; $Node Stack[max_n], *ptr[max_n], *Store[max_n]; -     voidInitintN) { -Tail = &stack[0]; the         NULL= tail++; -         NULL-Set();Wuyin = n, top =0, Rec.init (n); the     } -Inline Node *newnode (intv) { WuNode *p =NULL; -         if(!top) p = tail++; About         Elsep = store[--top]; $P->Set(V,0,NULL); -         returnp; -     } -Inline node* Merge (node* &x, node* &y) { A         if(x = =NULL)returny; +         if(Y = =NULL)returnx; the         if(Y->v > x->v) std::swap (x, y); -x->ch[1] = Merge (x->ch[1], y); $         if(x->ch[1]-&GT;NPL > x->ch[0]->NPL) theStd::swap (x->ch[0], x->ch[1]); theX->push_up (); the         returnx; the     } -InlineintGet_max (inti) { in         returnPtr[i]->v; the     } theInlinevoidInsert () { About         intv; the          for(inti =1; I <= N; i++){ thescanf"%d", &v); thePtr[i] =NewNode (v); +         } -     } theInlinevoidDelinti) {Bayi         intRET =Get_max (i); theNode *x = NewNode (ret >>1); thestore[top++] =Ptr[i]; -Ptr[i] = Merge (ptr[i]->ch[0], ptr[i]->ch[1]); -Ptr[i] =Merge (Ptr[i], x); the     } theInlinevoidGogointAintb) { the         intAns =0; theA = Rec.find (a), B =Rec.find (b); -         if(A = =b) { theprintf"-1\n"); the             return; the         }94 Rec.unite (A, b); the del (a), Del (b); the         if(Rec.rank[a] >Rec.rank[b]) { thePtr[a] =Merge (Ptr[a], ptr[b]);98Ans = ptr[a]->v; About}Else { -PTR[B] =Merge (Ptr[a], ptr[b]);101Ans = ptr[b]->v;102         }103printf"%d\n", ans);104     } the }lft;106 intMain () {107 #ifdef LOCAL108Freopen ("In.txt","R", stdin);109Freopen ("OUT.txt","w+", stdout); the #endif111     intN, M, A, b; the      while(~SCANF ("%d", &N)) {113 lft.init (n), Lft.insert (); thescanf"%d", &m); the          while(m--){ thescanf"%d%d", &a, &b);117 Lft.gogo (A, b);118         }119     } -     return 0;121}
View Code

Zoj 2334 Monkey king/left bias tree + and check set

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.