#BS0000061. Tổ tiên của tôi (My Ancestor)
Tổ tiên của tôi (My Ancestor)
My Ancestor
Source: Thailand ICPC National
Version: Phuoc Hung OJ Extended
Problem Statement
A rooted family tree has root vertex . Each vertex has a positive value . For every parent-child edge , , so values are strictly increasing on every root-to-vertex path.
For each query , find the proper ancestor of that is closest to the root and whose value is at least . Print if no such ancestor exists. The PHOJ version fixes root and explicitly lists the parent of every vertex .
Input
- The first line contains .
- The second line contains .
- The third line contains parents , with .
- Each of the next lines contains .
Output
For each query, print the required ancestor index or -1.
Subtasks
- Subtask 1 — 20%: , .
- Subtask 2 — 30%: , , .
- Subtask 3 — 50%: , , , and .
Example
Input
7 4
3 5 8 7 10 9 12
1 1 2 2 4 4
5 4
5 7
7 8
2 6
Output
2
-1
-1
-1
Explanation
For vertex , proper ancestors have values ; threshold selects vertex . For vertex , no proper ancestor reaches threshold .