eolymp
bolt
Try our new interface for solving problems
Problems

Company Queries I

Company Queries I

A company has $n$ employees, who form a tree hierarchy where each employee has a boss, except for the general director. Your task is to process $q$ queries of the form: who is employee $x$'s boss $k$ levels higher up in the hierarchy? \InputFile The first line has two integers $n$ and $q$: the number of employees and queries. The employees are numbered $1, 2, ..., n$, and employee $1$ is the general director. The next line has $n - 1$ integers $e_2, e_3, ..., e_n$: for each employee $2, 3, ..., n$ their boss. Finally, there are $q$ lines describing the queries. Each line has two integers $x$ and $k$: who is employee $x$'s boss $k$ levels higher up? \OutputFile Print the answer for each query. If such a boss does not exist, print $-1$. \includegraphics{https://static.eolymp.com/content/ks/ksdtrerq1d3lpe2bgt0rua2o84.gif}
Time limit 1 second
Memory limit 128 MiB
Input example #1
5 3
1 1 3 3
4 1
4 2
4 3
Output example #1
3
1
-1