#P3224. [HNOI2012] 永无乡
[HNOI2012] 永无乡
Description
Neverland contains islands, numbered from to . Each island has its own unique importance. According to importance, these islands can be ranked, with ranks from to . Some islands are connected by large bridges, through which one can travel from one island to another. If starting from island you can reach island after crossing several (including ) bridges, then islands and are said to be connected.
There are two operations:
B x y means building a new bridge between island and island .
Q x k asks which island currently ranks the -th by importance among all islands connected to island , i.e., among all islands connected to , which island has the -th smallest importance rank. Please output the index of that island.
Input Format
The first line contains two integers separated by a space, representing the number of islands and the initial number of bridges .
The second line contains integers; the -th integer is the rank of island .
The next lines each contain two integers , indicating that initially there is a bridge connecting island and island .
The next line contains an integer, the number of operations .
The next lines each describe an operation. Each line first contains a character indicating the operation type, followed by two integers .
- If is
Q, it asks which island ranks the -th smallest by importance among all islands connected to ; output the index of that island. - If is
B, it means building a new bridge between island and island .
Output Format
For each query operation, output one integer on a separate line, indicating the index of the island being asked for. If such an island does not exist, output .
5 1
4 3 2 5 1
1 2
7
Q 3 2
Q 2 1
B 2 3
B 1 5
Q 2 1
Q 2 4
Q 2 3
-1
2
5
1
2
Hint
Constraints
- For of the testdata, , .
- For of the testdata, , , is a permutation of , , .
Translated by ChatGPT 5
京公网安备 11011102002149号