"HDU 1512" Monkey King

Source: Internet
Author: User

Monkey KingTime limit:10000/5000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 3423 Accepted Submission (s): 1479


Problem descriptiononce in a forest, there lived N aggressive monkeys. At the beginning, they each does things on its own the and none of the them knows each other. But monkeys can ' t avoid quarrelling, and it is only happens between the monkeys of who does the know each other. And when it happens, both the monkeys would invite the strongest friend of them, and duel. Of course, after the duel, the both monkeys and all of there friends knows each other, and the quarrel above would no longer Happens between these monkeys even if they have ever conflicted.

Assume that every money have a strongness value, which'll be reduced to only half of the original after a duel (that is, 1 0 'll be reduced to 5 and 5 would be reduced to 2).

And we also assume that every monkey knows himself. That was, when he was the strongest one in all of his friends, he himself would go to duel.

Inputthere is several test cases, and each case consists of the parts.

First Part:the first line contains an integer N (n<=100,000), which indicates the number of monkeys. And then N lines follows. There is a number on each line, indicating the strongness value of ith monkey (<=32768).

Second part:the First line contains an integer m (m<=100,000), which indicates there is M conflicts happened. And then M lines follows, each line of which contains the integers x and Y, indicating that there is a conflict between th E Xth Monkey and Yth.


Outputfor each of the conflict, output-1 if the and monkeys know each other, otherwise output the strongness value of the Strongest monkey in all friends of them after the duel.
Sample Input
520161010452 33 43 54 51 5

Sample Output
855-110

Authorjiang, Yanyan
Sourcezoj 3rd Anniversary Contest
Recommendlinle

Left-leaning tree + and check set.


The explanation of the left-leaning tree by Baidu Library


Mato Blog:



This question determines whether the output-1, use and check set can be.


For combat effectiveness halved, first delete the original, and then halved after the point and the original tree merge.


Merging is the basic operation of the left-leaning tree, merging two stacks.


#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include < Cstdlib> #define M 100005using namespace Std;int fa[m],n,m;struct tree{int l,r,dis,v;} T[m];int getfather (int x) {if (x==fa[x]) return X;return Fa[x]=getfather (fa[x]);} int Merge (int x,int y) {if (x==0) return y;if (y==0) return x;if (T[X].V&LT;T[Y].V) swap (x, y), T[x].r=merge (t[x].r,y); int l= T[x].l,r=t[x].r;fa[r]=x;if (T[l].dis<t[r].dis) Swap (T[X].L,T[X].R), if (!T[X].R) T[x].dis=0;else t[x].dis=t[t[x]. R].dis+1;return x;} int Del (int x) {int L,r;l=t[x].l,r=t[x].r;fa[l]=l,fa[r]=r;t[x].l=t[x].r=t[x].dis=0;return Merge (l,r);} void Solve (int x,int y) {t[x].v/=2,t[y].v/=2;int le,ri;le=del (x), Ri=del (y), Le=merge (le,x), Ri=merge (Ri,y), Le=merge ( Le,ri);p rintf ("%d\n", T[LE].V);} int main () {while (scanf ("%d", &n)!=eof) {for (int i=1;i<=n;i++) {scanf ("%d", &t[i].v); t[i].l=t[i].r=t[i].di S=0;fa[i]=i;} scanf ("%d", &m), for (int i=1;i<=m;i++) {int x,y;scanf ("%d%d", &x,&y); int fx=Getfather (x), Fy=getfather (y), if (Fx==fy) puts ("1"); Elsesolve (Fx,fy);}} return 0;}


"HDU 1512" Monkey King

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.