Distinct weights on a tree path
Time limit2sMemory limit512 MB
- Level
Not classified yet
- Solved
- No attempts yet
Problem
You are given a tree with vertices. A tree is a connected undirected graph with no cycle. The vertices are numbered from 1 to and the edges are numbered from 1 to . Every vertex carries one weight.
Write a program that answers the following query.
u v: print how many different weight values appear among the vertices on the path from vertex to vertex . The path includes both endpoints and .
Input
The first line contains the number of vertices . ()
The second line contains the weights of vertices 1 through in order. Every weight is a positive integer no larger than .
Each of the next lines contains two vertex numbers and joined by edge . The given edges always form a single tree.
The next line contains the number of queries . ()
Each of the next lines contains one query in the form u v. Both and are between 1 and , and queries where the two values are equal also occur.
Output
Print the answer to each query on its own line, in the order the queries are given.