码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1512 Monkey King

时间:2015-03-12 11:20:43      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

Monkey King

Time Limit: 5000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 1512
64-bit integer IO format: %I64d      Java class name: Main
 
Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its own way and none of them knows each other. But monkeys can‘t avoid quarrelling, and it only happens between two monkeys who does not know each other. And when it happens, both the two monkeys will invite the strongest friend of them, and duel. Of course, after the duel, the two monkeys and all of there friends knows each other, and the quarrel above will no longer happens between these monkeys even if they have ever conflicted.

Assume that every money has a strongness value, which will be reduced to only half of the original after a duel(that is, 10 will be reduced to 5 and 5 will be reduced to 2).

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

Input

There are several test cases, and each case consists of two 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 one 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 are M conflicts happened. And then M lines follows, each line of which contains two integers x and y, indicating that there is a conflict between the Xth monkey and Yth.

Output

For each of the conflict, output -1 if the two monkeys know each other, otherwise output the strongness value of the strongest monkey in all friends of them after the duel.

Sample Input

5
20
16
10
10
4
5
2 3
3 4
3 5
4 5
1 5

Sample Output

8
5
5
-1
10

Source

 
解题:左偏树。。。
技术分享
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5 const int maxn = 200010;
 6 struct node{
 7     int l,r,f,value,dis;
 8 };
 9 node heap[maxn];
10 int mergeIt(int a,int b){
11     if(a == 0) return b;
12     if(b == 0) return a;
13     if(heap[a].value < heap[b].value) swap(a,b);
14     heap[a].r = mergeIt(heap[a].r,b);
15     heap[heap[a].r].f = a;
16     if(heap[heap[a].l].dis < heap[heap[a].r].dis) swap(heap[a].l,heap[a].r);
17     if(heap[a].r == 0) heap[a].dis = 0;
18     else heap[a].dis = heap[heap[a].r].dis+1;
19     return a;
20 }
21 int pop(int a){
22     int l = heap[a].l;
23     int r = heap[a].r;
24     heap[l].f = l;
25     heap[r].f = r;
26     heap[a].l = heap[a].r = heap[a].dis = 0;
27     return mergeIt(l,r);
28 }
29 int findF(int a){
30     return heap[a].f = heap[a].f == a?a:findF(heap[a].f);
31 }
32 int main(){
33     int n,m;
34     while(~scanf("%d",&n)){
35         for(int i = 1; i <= n; ++i){
36             scanf("%d",&heap[i].value);
37             heap[i].l = heap[i].r = heap[i].dis = 0;
38             heap[i].f = i;
39         }
40         scanf("%d",&m);
41         while(m--){
42             int fa,fb,a,b;
43             scanf("%d %d",&a,&b);
44             fa = findF(a);
45             fb = findF(b);
46             if(fa == fb) puts("-1");
47             else{
48                 heap[fa].value >>= 1;
49                 int u = pop(fa);
50                 u = mergeIt(u,fa);
51 
52                 heap[fb].value >>= 1;
53                 int v = pop(fb);
54                 v = mergeIt(v,fb);
55 
56                 printf("%d\n",heap[mergeIt(u,v)].value);
57             }
58         }
59     }
60     return 0;
61 }
View Code

 

HDU 1512 Monkey King

标签:

原文地址:http://www.cnblogs.com/crackpotisback/p/4331799.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!