#CC0000072. Giá trị nhỏ thứ k trên đường đi của cây (COT - Count on a tree)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Giá trị nhỏ thứ k trên đường đi của cây (COT - Count on a tree)
COT - Count on a tree
Source: SPOJ
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a tree with vertices, each with an integer weight . For query , consider all vertex weights on the unique path from to , including both endpoints, and print the -th smallest weight. Repeated weights count with multiplicity.
Input
The first line contains . The second contains weights. Then follow tree edges , and queries with the number of vertices on the path.
Output
For each query, print the -th smallest weight on the specified path.
Subtasks
Original constraints: ; ; . In this PHOJ dataset, (the SPOJ statement does not give an explicit weight bound).
- Subtask 1 (20 points): ; ; ; valid tree and path queries.
- Subtask 2 (30 points): ; ; ; valid tree and path queries.
- Subtask 3 (50 points): ; ; ; valid tree and path queries.
Examples
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
The path from vertex to vertex has weights , which sort to . This explains the first four answers.