#P2486. [SDOI2011] 染色
[SDOI2011] 染色
Description
Given an unrooted tree with nodes, there are operations of two types:
- Color all nodes on the path from node to node (including and ) with color .
- Query the number of color segments on the path from node to node .
A color segment is defined as a maximal consecutive run of the same color. For example, 112221 consists of three segments: 11, 222, 1.
Input Format
The first line contains two integers separated by a space, representing the number of nodes and the number of operations .
The second line contains integers separated by spaces. The -th integer represents the initial color of node .
Lines through each contain two integers separated by a space, indicating that there is an edge between nodes and in the tree.
Lines through each describe an operation in the following format:
Each line starts with a character , indicating the type of operation.
- If is
C, it denotes a coloring operation. After a space, there are three integers separated by spaces, meaning that all nodes on the path from to are colored with color . - If is
Q, it denotes a query operation. After a space, there are two integers separated by a space, indicating a query for the number of color segments on the path from to .
Output Format
For each query operation, output a single line containing one integer representing the answer.
6 5
2 2 1 2 1 1
1 2
1 3
2 4
2 5
2 6
Q 3 5
C 2 1 1
Q 3 5
C 5 1 2
Q 3 5
3
1
2
Hint
Constraints
For of the testdata, , , , is guaranteed to be C or Q, and the given graph is a tree.
In addition to the original testdata, there exists a non-scoring set of hack testdata.
Translated by ChatGPT 5
京公网安备 11011102002149号