#SGM0000072. Đếm trên cây (Count on a tree)
Đếm trên cây (Count on a tree)
Count on a tree
Source: SPOJ
Version: Phuoc Hung OJ Extended
Problem
A tree has weighted vertices. Query asks for the -th smallest vertex weight on the simple path from to , including both endpoints.
Input
The first line contains , then the vertex weights, edges, and finally queries .
Output
Print the -th smallest weight for each path query.
Subtasks
Subtask 1 (20%)
- , number of queries .
- All other conditions are the same as Subtask 3.
Subtask 2 (30%)
- , number of queries .
- All other conditions are the same as Subtask 3.
Subtask 3 (50%)
- vertex weights are integers
- the number of vertices on path
Example
Input
8 5
105 2 9 3 8 5 7 7
1 2
1 3
1 4
3 5
3 6
3 7
4 8
2 5 1
2 5 2
2 5 3
2 5 4
7 8 2
Output
2
8
9
105
7
Explanation
Path 2-1-3-5 has weights [2,105,9,8], which sort to [2,8,9,105].