P19188 [HackerRank] Jim and his lan party
提高-
题目描述

During the Steam Summer Sale, Jim's $N-1$ friends have purchased $\mbox{M}$ games, which are numbered from $\mbox{1}$ to $\mbox{M}$. The games are multiplayer. Jim has invited his friends to his basement where they will play by making a LAN-Party.

Each friend has already decided the game he would like to play for the rest of the day. So there will be a group of friends who will play the same game together.

But then, they face a problem: Currently, none of the friends' PCs are connected. So they have to be connected using the available $\mbox{Q}$ wires. Jim decides to connect friends $u_i$ and $v_i$ with the $\boldsymbol{i}^{th}$ wire one by one. So he starts with wire 1, then with wire 2 and so on.

A group can start playing their game, only if all the members are connected (if not directly, then there must exist a path connecting them). They want to start playing as soon as possible.

For each game, find out the wire after adding which the group can start playing. It is also possible that a group will never get connected. In such a case, this group starts crying and you should display -1.

Input Format

On the first line there will be $N$, $\mbox{M}$ and $\mbox{Q}$ each separated by a single space. On the second line we will give you $N$ integers separated by a single space: The $\boldsymbol{i}$-th integer denotes the game friend $\boldsymbol{i}$ wants to play (all between $\mbox{1}$ and $\mbox{M}$). The next $\mbox{Q}$ lines will denote $\mbox{Q}$ wires: $i^{th}$ line denotes $i^{th}$ wire and is denoted by $u_i$ and $v_i$ pairs each separated by a single space.

Constraints

$1\leq N,M\leq10^5$ For each game $\boldsymbol{i}$, the number of players playing $\boldsymbol{i}$ will be positive.

$0\leq Q\leq10^5$

Note
Each game is chosen by at least one player. If a group consists of only one member, then print 0, since this lucky (?) lone player can start right away!

Output Format

Print on the $\boldsymbol{i}^{th}$line the answer for the $\boldsymbol{i}^{th}$game.

Sample Input
5 2 4
1 2 2 2 1
1 2
2 3
1 5
4 5

Sample Output
3
4

Explanation

The group with the game 1 can play after the 3^{rd} wire is added. The group with game 2 can play only after the 4^{th} wire has been added because after adding the 4^{th} wire, a path between (2,3) (3,4) and (2,4) gets created.

输入格式

见题面

输出格式

见题面

样例输入
5 2 4 1 2 2 2 1 1 2 2 3 1 5 4 5
样例输出
3 4