#P14734. [ICPC 2021 Seoul R] Ant Colonies
[ICPC 2021 Seoul R] Ant Colonies
题目描述
A group of scientists analyzed an ant nest where several ant colonies live. They found that the ant nest is a tree structure in which each node represents the physical place where an ant colony lives, and each edge represents a tunnel connecting two ant colonies. The most interesting thing is that each colony has exactly one color and it sometimes changes its color. The color change mechanism depends on the closest pair of colonies with a certain color among the colonies lying on the path between two given colonies and . The distance between two colonies is the number of tunnels of the path connecting them, that is, the number of edges of the path connecting two corresponding nodes in the tree structure.
For example, Figure A.1 (a) shows a tree structure with five ant colonies numbered 1 to 5 of colors , labelled in orange above the colonies, in order from colony 1 to colony 5, respectively. For color and two colonies and , the closest pair of colonies with color on the path between colony and colony is the pair (colony , colony ). But for colony and colony , the closest pair with color is the pair (colony , colony ).
Suppose now that the current color of colony changes to color as shown in Figure A.1 (b). Then there is no closest pair of colonies with color on the path between colony and colony because only one colony has color . The closest pair with color for colony and colony becomes (colony , colony ).
Given colors of ant colonies, a tree structure of the ant nest, and an ordered list of update commands for the color change and query commands for the closest pair, write a program to find the closest pair of colonies with color between the two colonies and for each query .
:::align{center}

Figure A.1 An ant nest with five colonies. The numbers in orange represent colony colors. :::
输入格式
Your program is to read from standard input. The input starts with a line containing two integers, and (, ), where is the number of ant colonies and is the number of update and query commands. Ant colonies are numbered from to , and colors are identified with integers from . The next line consists of positive integers representing colors for ant colonies, in order from colony to colony . In the following lines, the -th line contains a pair of integers () specifying the numbers of two ant colonies connected by a tunnel, which corresponds to an edge in the tree structure. In the following lines, the -th line has a form of or , where is a single uppercase character either 'U' or 'Q', representing the update and the query, respectively. In the case that , it has the form of which is an update command to change (update) the current color of colony to color (). In the case of , it has the form of which is a query command to output the distance of the closest pair of colonies with color on the path between colony and colony (). These commands must be executed in the order given in the input.
输出格式
Your program is to write to standard output. For every query with , print exactly one line containing the distance of the closest pair of colonies with color on the path between colonies and under the current status of the ant nest. If there is no pair with color between them, print .
5 5
1 2 2 2 1
1 2
3 1
3 4
3 5
Q 2 5 2
Q 2 4 2
U 3 3
Q 2 5 2
Q 2 4 2
2
1
-1
3
4 6
2 1 1 1
1 2
1 3
1 4
Q 2 3 1
Q 2 4 1
Q 3 4 1
U 1 1
Q 2 3 1
Q 2 4 1
2
2
2
1
1