#P7103. Lowest Common Ancestor of k-th Level Nodes

    ID: 20309 Type: Default 1000ms 256MiB

Lowest Common Ancestor of k-th Level Nodes

Lowest Common Ancestor of k-th Level Nodes

You are given a family tree where nodes are numbered from 1 to n. The root node has number 1 and is at depth 1. For any node with id i (i ≥ 2), its parent is given. You are also given an integer k. Your task is to find the Lowest Common Ancestor (LCA) of all nodes that are exactly at depth k in the tree. It is guaranteed that there is at least one node at depth k.

Formally, let the depth of the root be 1. For any node at depth k, find the node v such that v is an ancestor of every node at depth k and for any other node u with this property, the depth of u is less than or equal to that of v. In mathematical terms, if we denote the set of nodes at depth k by S, find LCA(S).

Note: The LCA of a single node is the node itself.

inputFormat

The first line contains two integers n and k (1 ≤ k ≤ n), where n is the number of nodes in the tree. The second line contains n-1 integers. The i-th integer (for i from 1 to n-1) represents the parent of node i+1. Node 1 is the root and has no parent.

outputFormat

Output a single integer denoting the lowest common ancestor of all nodes at depth k.

The answer is unique and is guaranteed to exist under the given input constraints.

sample

3 2
1 1
1