#abc239e. [abc239_e]Subtree K-th Max
[abc239_e]Subtree K-th Max
Problem Statement
We have a rooted tree with vertices. The vertices are numbered through , and the root is Vertex .
The -th edge connects Vertices and .
Vertex has an integer written on it.
You are given queries. For the -th query, given a pair of integers , answer the following question.
- Question: among the integers written on the vertices in the subtree rooted at Vertex , find the -th largest value.
Constraints
- The given graph is a tree.
- The subtree rooted at Vertex has or more vertices.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print lines. The -th line should contain the response to the -th query.
Sample Input 1
5 2
1 2 3 4 5
1 4
2 1
2 5
3 2
1 2
2 1
Sample Output 1
4
5
The tree given in this input is shown below.
For the -st query, the vertices in the subtree rooted at Vertex are Vertices , and , so print the -nd largest value of the numbers written on these vertices, .
For the -nd query, the vertices in the subtree rooted at Vertex are Vertices , and , so print the -st largest value of the numbers written on these vertices, .
Sample Input 2
6 2
10 10 10 9 8 8
1 4
2 1
2 5
3 2
6 4
1 4
2 2
Sample Output 2
9
10
Sample Input 3
4 4
1 10 100 1000
1 2
2 3
3 4
1 4
2 3
3 2
4 1
Sample Output 3
1
10
100
1000