#P8339. [AHOI2022] 钥匙

[AHOI2022] 钥匙

Problem Description

There are nn cities, numbered 1,2,,n1, 2, \ldots, n. These cities are connected by n1n - 1 undirected roads, each road connects two cities, and it is guaranteed that any two cities are connected. That is, the nn cities form a tree.

Each city has one treasure. Treasures are of two types: keys and chests. In a city, there is either a key or a chest. Keys and chests have different colors: a key of color ii can only open a chest of color ii. After opening a chest, you can obtain one gold coin, and the key will break.

Due to some special reason, for the same type (same color), there are at most 5\bm{5} keys (the number of chests of the same color is unlimited).

Now Xiao R plans mm trips. For the ii-th trip, the start is sis_i and the end is eie_i. Xiao R walks from sis_i to eie_i along the shortest path. When he arrives at a city with a key, he may put the key into his backpack. When he arrives at a city with a chest, if he has a key of the corresponding color, he will open the chest and get one gold coin; if he does not have the corresponding key, he does nothing (the chest cannot be taken away). Ask how many gold coins can be obtained in each trip.

Note: Trips are independent. After one trip ends, all keys and chests will be restored to their initial state.

Input Format

The first line contains two positive integers n,mn, m, representing the number of cities and the number of trips.

In the next nn lines, each line contains two positive integers ti,cit_i, c_i. ti=1t_i = 1 means there is a key in city ii, and ti=2t_i = 2 means there is a chest. cic_i is the color of the key or chest in city ii. The testdata guarantees that for each color, there are no more than 55 keys.

In the next n1n - 1 lines, each line contains two positive integers ui,viu_i, v_i, indicating an undirected road connecting uiu_i and viv_i.

In the next mm lines, each line contains two positive integers si,eis_i, e_i, representing one trip's start and end.

Output Format

Output mm lines. Each line contains one integer, representing the number of gold coins obtainable in the ii-th trip.

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

1
1
1

见附件中的 keys/keys2.in
见附件中的 keys/keys2.ans
见附件中的 keys/keys3.in
见附件中的 keys/keys3.ans

Hint

[Sample #4]

See keys/keys4.in and keys/keys4.ans in the attachment.

This sample satisfies n,m105n, m \le {10}^5 and Special Property A.

[Constraints]

For 100%100\% of the testdata, 1n5×1051 \le n \le 5 \times {10}^5, 1m1061 \le m \le {10}^6, 1ti21 \le t_i \le 2, 1ci,ui,vi,si,ein1 \le c_i, u_i, v_i, s_i, e_i \le n, and for each color, there are at most 55 keys.

Test Point ID nn \le mm \le Special Property
11 100100 None
232 \sim 3 50005000
454 \sim 5 105{10}^5
686 \sim 8 5×1055 \times {10}^5 106{10}^6 A
9109 \sim 10 None

Special Property A: For each color that appears, there is exactly one key and one chest of that color.

[Hint]

The input and output data are large, so please use a faster I/O method.

Translated by ChatGPT 5