영어 원문입니다. 원문의 수식·표를 글자로 옮겨 모양이 다를 수 있습니다. 정확한 내용은 원문 페이지에서 확인하세요. 원문 페이지 열기 (새 창)
시간 제한 1초 · 메모리 제한 512 MB
Problem
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 the lowest common boss of employees a and b 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,…,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 a and b: who is the lowest common boss of employees a and b?
Output
Print the answer for each query.
Constraints
- 1 ≤ n,q ≤ 2 · 10^5
- 1 ≤ e_i ≤ i-1
- 1 ≤ a,b ≤ n
예제
예제 1
5 3 1 1 3 3 4 5 2 5 1 4
3 1 1