P8744 [Lanqiao Cup 2021 NOI Qualifier A] Left Child Right Sibling

Description

For a multiway tree, we can convert it into a binary tree using the “left child right sibling” representation. If we consider the children of each node to be unordered, then the resulting binary tree may not be unique. In other words, for each node, we may choose any child as its left child, and connect the remaining children as right siblings in any order. Given a multiway tree with $N$ nodes, numbered from $1$ to $N$, where node $1$ is the root, and the index of each node’s parent is smaller than the node’s own index, compute the maximum possible height of the binary tree obtained by the “left child right sibling” representation. (A tree with only the root node has height $0$.) For example, the following multiway tree: ![](https://luogu.oss-cn-hangzhou.aliyuncs.com/upload/vjudge_pic/lanqiao/2022_09_29_d8f144a59f4c0ce9322ag-11.jpg) may have the following $3$ different “left child right sibling” representations (only $3$ are listed here, not all of them): ![](https://luogu.oss-cn-hangzhou.aliyuncs.com/upload/vjudge_pic/lanqiao/2022_09_29_d8f144a59f4c0ce9322ag-12.jpg) The last one has the greatest height, which is $4$.

Input Format

The first line contains an integer $N$. The next $N - 1$ lines each contain an integer, in order giving the parent index of nodes $2$ to $N$.

Output Format

Output one integer representing the answer.

Explanation/Hint

For $30\%$ of the test cases, $1 \leq N \leq 20$. For all test cases, $1 \leq N \leq 10^5$. Lanqiao Cup 2021, first round, provincial contest, Group A, Problem H. Translated by ChatGPT 5