#P8339. [AHOI2022] 钥匙
[AHOI2022] 钥匙
Problem Description
There are cities, numbered . These cities are connected by undirected roads, each road connects two cities, and it is guaranteed that any two cities are connected. That is, the 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 can only open a chest of color . 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 keys (the number of chests of the same color is unlimited).
Now Xiao R plans trips. For the -th trip, the start is and the end is . Xiao R walks from to 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 , representing the number of cities and the number of trips.
In the next lines, each line contains two positive integers . means there is a key in city , and means there is a chest. is the color of the key or chest in city . The testdata guarantees that for each color, there are no more than keys.
In the next lines, each line contains two positive integers , indicating an undirected road connecting and .
In the next lines, each line contains two positive integers , representing one trip's start and end.
Output Format
Output lines. Each line contains one integer, representing the number of gold coins obtainable in the -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 and Special Property A.
[Constraints]
For of the testdata, , , , , and for each color, there are at most keys.
| Test Point ID | Special Property | ||
|---|---|---|---|
| None | |||
| A | |||
| 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