#P8744. [蓝桥杯 2021 省 A] 左孩子右兄弟
[蓝桥杯 2021 省 A] 左孩子右兄弟
Problem 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 nodes, numbered from to , where node 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 .)
For example, the following multiway tree:

may have the following different “left child right sibling” representations (only are listed here, not all of them):

The last one has the greatest height, which is .
Input Format
The first line contains an integer .
The next lines each contain an integer, in order giving the parent index of nodes to .
Output Format
Output one integer representing the answer.
5
1
1
1
2
4
Hint
For of the test cases, .
For all test cases, .
Lanqiao Cup 2021, first round, provincial contest, Group A, Problem H.
Translated by ChatGPT 5