Kth ancestor in tree
Your task
You are given a rooted tree of n nodes labeled 0..n-1 as a parent array (parent[root] = -1). Answer a batch of kth-ancestor queries: for [node, k] return the label of the ancestor reached by going up k edges, or -1 if it does not exist (you walk past the root). Precompute jump pointers (up[j][v] = the 2^j-th ancestor) so each query decomposes k into its binary bits and resolves in O(log n). Return the list of answers in query order.
Examples
kth_ancestor(n: int, parent: list[int], queries: list[list[int]]) -> list[int]Example 1
Input
kth_ancestor(7, [-1, 0, 0, 1, 1, 2, 2], [[3, 1], [3, 2], [5, 2], [3, 5]])Output
[1, 0, 0, -1]