Zoo
Time limit5sMemory limit1024 MB
Given a tree of enclosures labeled with species, answer each query with the highest count of one species on the path between two enclosures.
- Level
Hard8 of 10
- Topics
- Tree, Sliding window, Hash map
- Solved
- No attempts yet
Problem
Jozo started working at a newly built zoo earlier this year. Today he received a special assignment: find the cause of the steady drop in visitors. While watching the enclosures and the paths between them, Jozo suspected that the tours are too boring, that is, that the zoo tours are too uniform.
The zoo has enclosures numbered from to and paths, each connecting two enclosures. There is a path between every pair of enclosures (not necessarily a direct one). Each enclosure holds animals of one species. Enclosure holds animals of species .
Jozo asked you to help him determine the uniformity of a path. The uniformity of a path is the number of occurrences of the most common animal among the enclosures on that path, including the enclosures at both ends. For example, if a path passes through enclosures with animals , , , , , , the uniformity is three, because animal appears three times.
For each of the pairs of enclosures , find the uniformity of the (shortest) path from enclosure to enclosure .
Input
The first line contains natural numbers and (, ).
The second line contains natural numbers (), the species of the animals in enclosure .
Each of the next lines contains two natural numbers and (), the two enclosures connected by the -th path.
Each of the next lines contains two natural numbers and (), describing the path from enclosure to enclosure whose uniformity must be found.
Output
Print lines. On the -th line, print the answer to the -th question.
Hint
This explains the first sample. Each node shows the enclosure number and the animal species.

Query 1: the enclosures on the path are 1 and 2. Species 1 and 2 each appear once, so the uniformity is .
Query 2: the enclosures on the path are 1, 2, and 4. Species 1 appears twice, so the uniformity is .
Query 3: the enclosures on the path are 4, 2, 1, 3, and 7. Species 1 appears three times, in enclosures 1, 4, and 7.
Query 4: the enclosures on the path are 2, 1, and 3. Species 2 appears twice and species 1 appears once.
Query 5: the enclosures on the path are 5, 2, 1, 3, and 6. Species 2 appears four times and species 1 appears once.