1512 Monkey King

Posted HWIM

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1512 Monkey King相关的知识,希望对你有一定的参考价值。

Monkey King

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6667    Accepted Submission(s): 2858


Problem Description

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
 

分析

左偏树,对于每次操作,取出最大的元素,将它从删除(即合并他的两个子树),除2后在加入进去。然后操作完成后,将两棵树合并即可。

code

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 
 5 using namespace std;
 6 
 7 const int N = 100100;
 8 
 9 int val[N],dis[N],ls[N],rs[N],fa[N];
10 
11 inline void read(int &x) {
12     x = 0;int f = 1;char ch = getchar();
13     for (; ch<0||ch>9; ch = getchar()) if (ch==-) f = -1;
14     for (; ch>=0&&ch<=9; ch = getchar()) x = x * 10 + ch - 0;
15     x = x * f;
16 }
17 int merge(int x,int y) {
18     if (!x || !y) return x + y;
19     if (val[x]<val[y]||(val[x]==val[y]&&x<y)) swap(x,y);
20     rs[x] = merge(rs[x],y);
21     fa[rs[x]] = x;
22     if (dis[ls[x]] < dis[rs[x]]) swap(rs[x],ls[x]);
23     if (rs[x]) dis[x] = dis[rs[x]] + 1;
24     else dis[x] = 0;
25     return x;
26 }
27 inline int find(int x) {
28     if (x==fa[x]) return x;
29     return fa[x] = find(fa[x]);
30 }
31 inline int work(int x) {
32     fa[ls[x]] = ls[x];fa[rs[x]] = rs[x]; //-
33     int t = merge(ls[x],rs[x]);
34     ls[x] = rs[x] = 0; //-
35     val[x] /= 2;
36     return merge(x,t);
37 }
38 int main() {
39     int m,n,a,b;
40     while (~scanf("%d",&n)) {
41         memset(rs,0,sizeof(rs));
42         memset(ls,0,sizeof(ls));
43         memset(dis,0,sizeof(dis));
44         for (int i=1; i<=n; ++i) read(val[i]),fa[i] = i;
45         read(m);
46         while (m--) {
47             read(a),read(b);
48             int x = find(a),y = find(b);
49             if (x == y) puts("-1");
50             else printf("%d\n",val[merge(work(x),work(y))]);
51         }
52     }
53     return 0;
54 }

 

 

以上是关于1512 Monkey King的主要内容,如果未能解决你的问题,请参考以下文章

1512 Monkey King

hdu_1512 Monkey King

[HDU1512]:Monkey King

hdu1512 Monkey King

HDU 1512 Monkey King

Monkey King HDU - 1512 (左偏树)