PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#LCA0000065. Giá trị lớn thứ k trong cây con (Subtree K-th Max)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Giá trị lớn thứ k trong cây con (Subtree K-th Max)
Subtree K-th Max
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
In a tree rooted at , vertex has integer value . For each query , take the values of all vertices in the subtree of , sort them in non-increasing order, and return the -th value (1-based). Equal values remain as separate occurrences. Every queried subtree contains at least vertices.
Input
First line: . Second line: values . Then edges and lines .
Output
Print the requested value on its own line for each query.
Subtasks
General constraints: ; ; ; ; không vượt kích thước cây con.
-
Subtask 1 (20 points): ; .
-
Subtask 2 (30 points): ; .
-
Subtask 3 (50 points): ; .
Examples
Input
5 2
1 2 3 4 5
1 4
2 1
2 5
3 2
1 2
2 1
Output
4
5
Explanation
Subtree has values , whose second-largest value is . Subtree has , whose largest value is .