#P1456. Monkey King

Monkey King

题目描述

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.

输入格式

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.

输出格式

For each of the conflict, output -1 if the two monkeys know each other, otherwise output the strength value of the strongest monkey among all of its friends after the duel.

题目大意

题目描述

曾经在一个森林中居住着 NN 只好斗的猴子。在最初他们我行我素,互不认识。但是猴子们不能避免争吵,且两只猴子只会在不认识对方时发生争吵,当争吵发生时,双方会邀请它们各自最强壮的朋友并发起决斗(决斗的为各自最强壮的朋友)。当然,在决斗之后两只猴子和他们各自的伙伴都认识对方了(成为朋友),虽然他们曾经有过冲突,但是他们之间绝不会再发生争吵了。

假设每只猴子有一个强壮值,强壮值将在一场决斗后减少为原先的一半(例如 1010 会减少到 55,而 55 会减少到 22,即向下取整)。

我们也假设每只猴子都认识它自己(是自己的朋友)。即当他是他朋友中最强壮的,他自己就会去决斗。

输入格式

有多组数据,每一组数据有两部分。

第一部分:第一行包含一个整数 NN 表示猴子的数量。后为 NN 行,每行一个数字为第 ii 只猴子的强壮值 sis_{i}

第二部分:第一行包含一个整数 MM 表示发生了 MM 次冲突。后为 MM 行,每行两个整数 xxyy,表示第 xx 只猴子和第 yy 只猴子之间发生了冲突。

输出格式

对于每次冲突,如果两只猴子认识对方,输出 -1,否则输出决斗后他们朋友中最强壮的猴子的强壮值。

说明/提示

N,M100000N,M\leq 100000si32768s_{i}\leq 32768

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

8
5
5
-1
10

提示

题目可能有多组数据