# Monkey King

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

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

Author
JIANG, Yanyan

Source

Recommend
linle

1. #include <iostream>
2. #include<cstdio>
3. #include<cstring>
4. #include<algorithm>
5. using namespace std;
6. const int N = 100005;
7. int m,n;
8. int set[N];
9. struct node
10. {
11.     int l,r,dis,key;
12. }tree[N];
13.
14. int find(int a)
15. {
16.     int root = a;
17.     while(root != set[root])
18.         root = set[root];
19.     int parent = set[a];
20.     while(parent != root)//路径压缩
21.     {
22.         set[a] = root;
23.         a = parent;
24.         parent = set[a];
25.     }
26.     return root;
27. }
28.
29. int merge(int a,int b)
30. {
31.     if(!a)
32.         return b;
33.     if(!b)
34.         return a;
35.     if(tree[a].key < tree[b].key)//大堆
36.         swap(a,b);
37.     tree[a].r = merge(tree[a].r,b);
38.     set[tree[a].r] = a;//并查
39.     if(tree[tree[a].l].dis < tree[tree[a].r].dis)
40.         swap(tree[a].l,tree[a].r);
41.     if(tree[a].r)
42.         tree[a].dis = tree[tree[a].r].dis + 1;
43.     else
44.         tree[a].dis = 0;
45.     return a;
46. }
47.
48. int pop(int a)
49. {
50.     int l = tree[a].l;
51.     int r = tree[a].r;
52.     set[l] = l;//因为要暂时删掉根，所以左右子树先作为根
53.     set[r] = r;
54.     tree[a].l = tree[a].r = tree[a].dis = 0;
55.     return merge(l,r);
56. }
57.
58. int nextint()
59. {
60.     char c;
61.     int ret = 0;
62.     while((c = getchar()) > '9' || c < '0')
63.         ;
64.     ret = c - '0';
65.     while((c = getchar()) >= '0' && c <= '9')
66.         ret = ret * 10 + c - '0';
67.     return ret;
68. }
69.
70. void print(int a)
71. {
72.     if(!a)
73.         return;
74.     print(a/10);
75.     putchar(a%10 + '0');
76. }
77.
78. int main()
79. {
80.     int a,b,i;
81.     while(~scanf("%d",&n))
82.     {
83.         for(i = 1;i <= n;i ++)
84.         {
85.             //scanf("%d",&tree[i].key);
86.             tree[i].key = nextint();
87.             set[i] = i;
88.             tree[i].l = tree[i].r = tree[i].dis = 0;
89.         }
90.        // scanf("%d",&m);
91.         m = nextint();
92.         while(m --)
93.         {
94.             //scanf("%d%d",&a,&b);
95.             a = nextint();
96.             b = nextint();
97.             int ra = find(a);
98.             int rb = find(b);
99.             //printf("%d  %d\n",ra,rb);
100.             if(ra == rb)
101.                 printf("-1\n");
102.             else
103.             {
104.                 int rra = pop(ra);//ra左右子树合并
105.                 tree[ra].key /= 2;
106.                 ra = merge(rra,ra);//重新插入ra 找到合适的位置
107.                 int rrb = pop(rb);
108.                 tree[rb].key /= 2;
109.                 rb = merge(rrb,rb);
110.                 print(tree[merge(ra,rb)].key);
111.                 putchar(10);
112.                 //printf("%d\n",tree[merge(ra,rb)].key);
113.             }
114.         }
115.     }
116.     return 0;
117. }
118. //703MS 2244K  无优化
119. //250MS 2184K  输入优化
120. //203MS 2184K  输入输出优化
• 本文已收录于以下专栏：

## HDU1512 左偏树||斜堆

• nlj1999
• 2016年01月17日 16:27
• 353

## hdu1512 & zoj2334Monkey King (左偏树 + 并查集(非优化的朴素并查集))

• hnust_taoshiqian
• 2015年07月28日 21:30
• 427

## 左偏树+并查集 hdu1512 Monkey King

• qwb492859377
• 2015年10月27日 22:46
• 749

## 【左偏树+并查集】Monkey King HDU1512

Monkey King Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T...
• jiangzh7
• 2013年05月03日 17:26
• 484

## hdu1512:Monkey King（左偏树）

Description Once in a forest, there lived N aggressive monkeys. At the beginning, they each does th...
• qq_35649707
• 2017年04月04日 14:03
• 258

## HDU1512 Monkey King 左偏树

Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its o...
• q121999
• 2017年11月02日 21:03
• 46

## hdu1512 monkey king 左偏树

• Merc_A
• 2015年04月09日 23:27
• 310

## 【HDU1512】【左偏树】Monkey King 题解

Monkey KingOnce in a forest, there lived N aggressive monkeys. At the beginning, they each does thin...
• MaverickFW
• 2017年07月17日 21:07
• 216

## hdu1512 & zoj2334Monkey King(左偏树 + 并查集)

hdu1512 & zoj2334Monkey King(左偏树 + 并查集) 分类： 左偏树 数据结构2013-07-17 09:29 56人阅读 评论(0) 收藏 举报 数据结...
• pi9nc
• 2013年09月19日 11:03
• 2278

## ZOJ2334 HDU1512 Monkey King，左偏树

• neofung
• 2011年09月27日 20:01
• 1414

举报原因： 您举报文章：HDU1512 Monkey King【并查集+左偏树】 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)