#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 NN nodes, numbered from 11 to NN, where node 11 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 00.)

For example, the following multiway tree:

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

The last one has the greatest height, which is 44.

Input Format

The first line contains an integer NN.

The next N1N - 1 lines each contain an integer, in order giving the parent index of nodes 22 to NN.

Output Format

Output one integer representing the answer.

5
1
1
1
2
4

Hint

For 30%30\% of the test cases, 1N201 \leq N \leq 20.

For all test cases, 1N1051 \leq N \leq 10^5.

Lanqiao Cup 2021, first round, provincial contest, Group A, Problem H.

Translated by ChatGPT 5