PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#LCA0000010. Tổ tiên cao nhất đạt ngưỡng (Greatest Parent)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Tổ tiên cao nhất đạt ngưỡng (Greatest Parent)
Greatest Parent
Source: LightOJ
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a tree rooted at vertex with vertices . Each vertex has a value . The root's value is , and every child has a strictly greater value than its parent.
Each query provides a vertex and a threshold . Among the vertices on the path from root to , inclusive, find the vertex closest to the root whose value is at least .
The strict increase guarantees a unique answer.
Input
- The first line contains .
- The next lines describe vertex using its parent and value .
- The final lines contain .
Output
For each query print the index of the highest ancestor satisfying the threshold.
Subtasks
- Subtask 1 (20 points): ; ; ; ; ; ; ; .
- Subtask 2 (30 points): ; ; ; ; ; ; ; .
- Subtask 3 (50 points): ; ; ; ; ; .
Examples
Input
7 4
0 3
0 4
0 2
1 4
2 7
2 10
5 1
4 2
5 4
6 10
Output
0
1
2
6
Explanation
For query , root has value below the threshold, while vertex has value , so the answer is . Query returns vertex .