CSES - Company Queries I

Xem dạng PDF

Gửi bài giải

Điểm: 1,00 (OI)
Giới hạn thời gian: 1.0s
Giới hạn bộ nhớ: 512M
Input: stdin
Output: stdout

Nguồn bài:
https://cses.fi/problemset/task/1687
Dạng bài
Ngôn ngữ cho phép
C, C++, Java, Kotlin, Pascal, PyPy, Python, Scratch

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?

Input

  • The first input line has two integers ~n~ and ~q~: the number of employees and queries. The employees are numbered ~1,2,\dots,n~, and employee ~1~ is the general director.

The next line has ~n-1~ integers ~e\_2,e\_3,\dots,e\_n~: for each employee ~2,3,\dots,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?

Output

  • Print the answer for each query. If such a boss does not exist, print ~-1~.

Constraints

  • ~1 \le n,q \le 2 \cdot 10^5~
  • ~1 \le e\_i \le i-1~
  • ~1 \le x \le n~
  • ~1 \le k \le n~

Example Input

5 3
1 1 3 3
4 1
4 2
4 3

Example Output

3
1
-1

Bình luận

Hãy đọc nội quy trước khi bình luận.


Không có bình luận tại thời điểm này.