#P4689. [Ynoi Easy Round 2016] 这是我自己的发明

[Ynoi Easy Round 2016] 这是我自己的发明

Description

You are playing a galgame, and then your parents suddenly come in, so you pretend you are solving a data structure problem.

Given a tree with nn nodes. Each node has a weight. The initial root is 11.

There are mm operations of the following types:

1 x Change the root of the tree to xx.

2 x y Given two nodes x,yx, y, choose every node from the subtree of xx and every node from the subtree of yy, and count the number of pairs whose node weights are equal.

Input Format

The first line contains two integers n,mn, m.

The second line contains nn integers, where the ii-th integer is the node weight aia_i.

The next n1n-1 lines each contain two integers x,yx, y, indicating an edge.

The next mm lines each describe one operation.

Output Format

For each query, output one integer representing the answer.

5 5
1 2 3 4 5
1 2
1 3
3 4
3 5
2 4 5
2 1 5
2 3 5
1 5
2 4 5
0
1
1
1

Hint

Idea: nzhtl1477, Solution: nzhtl1477, Code: nzhtl1477, Data: nzhtl1477

Constraints
For 100%100\% of the testdata, 1n1051 \le n \le 10^5, 1m5×1051 \le m \le 5 \times 10^5, 1ai1091 \le a_i \le 10^9.

Translated by ChatGPT 5