#P10180. 半彩三重奏
半彩三重奏
Background

FanFan is not satisfied with building only a deep-blue tree. He thinks a tree with only deep blue is too monotonous, so he wants to dye this tree with colors.
Problem Description
Since it is now the fantasy Year of the Dragon, FanFan’s deep-blue tree has been dyed with colors, and the color of node is .
FanFan is someone who changes his mind easily. In the next days, he will change the colors he likes every day. On day , the two colors he likes are ().
But to take care of his tree, he needs to move on the tree often, and he will only pass through colors he likes.
Specifically, on day , FanFan will choose an ordered pair of nodes , then move along the unique simple path , and all nodes on the path (including and ) must have colors in ( may be equal to ). After that, he can do one Yoimiya pull.
Every day, you need to pull a C6 Yoimiya, and then tell FanFan how many possible ordered node pairs he can choose.
Input Format
The first line contains two positive integers .
The next line contains positive integers , representing the color of each node.
The next line contains positive integers , meaning that for all , there is an edge in the tree.
The next lines each contain two positive integers .
Output Format
For each query, output one positive integer per line representing the answer.
5 3
1 2 1 3 3
1 1 2 2
1 2
1 3
2 3
9
6
9
Hint
Sample Explanation
The shape of the tree is shown in the figure:

For the first query, the only valid paths are paths from one node to another among .
For the second query, the only valid paths are $1 \to 1, 1 \to 3, 3 \to 1, 3 \to 3, 4 \to 4, 5 \to 5$.
Subtask Constraints
This problem uses bundled subtasks.
For of the data, , , , . It is guaranteed that .
| Subtask ID | Special Property | Score | Dependencies | ||
|---|---|---|---|---|---|
| Subtask #1 | None | None | |||
| Subtask #2 | |||||
| Subtask #3 | A | None | |||
| Subtask #4 | B | ||||
| Subtask #5 | None | ||||
| Subtask #6 | |||||
| Subtask #7 | |||||
Special Property A: .
Special Property B: .
Translated by ChatGPT 5