AdvancedRating 1900
k-th Ancestor
The tree is rooted at node 1. For each query (v, k), print the node k steps above v, or -1 if v has fewer than k ancestors.
Input
n; n − 1 parents (of nodes 2 … n); q; q lines 'v k'.
Output
q lines.
Sample 1
Input
7 1 1 2 2 3 3 4 4 1 5 2 6 3 1 0
Output
2 1 -1 1
Sample 2
Input
7 1 1 3 3 5 5 20 3 3 1 2 5 0 5 2 5 2 1 0 7 3 7 2 3 3 2 0 2 0 4 3 7 2 2 0 6 0 2 1 7 2 1 3 3 0 4 3
Output
-1 -1 5 1 1 1 1 3 -1 2 2 -1 3 2 6 1 3 -1 3 -1
Output appears here