#P5669. [SDOI2018] 原题识别-改
[SDOI2018] 原题识别-改
Description
There is a rooted tree with nodes. The root is node , and node has color . You need to handle queries of the following two types:
-
: Query how many different colors appear on the shortest path from node to node in the tree.
-
: Randomly choose a node on the path from node to the root, and randomly choose a node on the path from node to the root. Compute the expected value of the answer to query . (The paths include , , and the root.)
For query type , let the answer be . Let be the number of nodes on the path from to the root, and let be the number of nodes on the path from to the root. You only need to output $\mathrm{ans}\cdot \mathrm{cnt_a}\cdot \mathrm{cnt_b}$.
Input Format
Note: The input format is different from the original problem.
Each test point contains only one test case.
The first line contains two non-negative integers , , representing the number of nodes and the number of queries.
The next line contains positive integers. The -th positive integer is , representing the color of node .
The next lines each contain two integers , , indicating that there is an edge between node and node . It is guaranteed that these edges form a tree.
The next lines each contain one query. The format and meaning of the queries are given in the description.
Output Format
Output lines. The -th line contains one non-negative integer, representing the answer to the -th query.
5 3
1 4 4 5 4
1 2
2 3
3 4
2 5
1 2 3
2 1 3
1 3 2
1
5
1
10 5
3 4 3 8 9 3 2 8 5 7
1 2
2 3
3 4
4 5
5 6
4 7
4 8
8 9
8 10
1 1 10
2 3 5
2 7 5
2 5 4
1 8 6
4
34
61
45
3
Hint
For all data, it is guaranteed that , . It is guaranteed that the input edges form a tree.
Subtask ( points): There are no type queries.
Subtask ( points): For every edge, .
Subtask ( points): There are no additional constraints.
Translated by ChatGPT 5
京公网安备 11011102002149号