题库练习 Hanging Hearts
← 上一题 下一题 →

A15403 | Hanging Hearts

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

Pak Chanek has $n$ blank heart-shaped cards. Card $1$ is attached directly to the wall while each of the other cards is hanging onto exactly one other card by a piece of string. Specifically, card $i$ ( $i > 1$ ) is hanging onto card $p_i$ ( $p_i < i$ ).

In the very beginning, Pak Chanek must write one integer number on each card. He does this by choosing any permutation $a$ of $[1, 2, \dots, n]$ . Then, the number written on card $i$ is $a_i$ .

After that, Pak Chanek must do the following operation $n$ times while maintaining a sequence $s$ (which is initially empty):

1. Choose a card $x$ such that no other cards are hanging onto it.
2. Append the number written on card $x$ to the end of $s$ .
3. If $x \neq 1$ and the number on card $p_x$ is larger than the number on card $x$ , replace the number on card $p_x$ with the number on card $x$ .
4. Remove card $x$ .

After that, Pak Chanek will have a sequence $s$ with $n$ elements. What is the maximum length of the longest non-decreasing subsequence $^\dagger$ of $s$ at the end if Pak Chanek does all the steps optimally?

$^\dagger$ A sequence $b$ is a subsequence of a sequence $c$ if $b$ can be obtained from $c$ by deletion of several (possibly, zero or all) elements. For example, $[3,1]$ is a subsequence of $[3,2,1]$ , $[4,3,1]$ and $[3,1]$ , but not $[1,3,3,7]$ and $[3,10,4]$ .

输入格式

The first line contains a single integer $n$ ( $2 \le n \le 10^5$ ) — the number of heart-shaped cards.

The second line contains $n - 1$ integers $p_2, p_3, \dots, p_n$ ( $1 \le p_i < i$ ) describing which card that each card hangs onto.

输出格式

Print a single integer — the maximum length of the longest non-decreasing subsequence of $s$ at the end if Pak Chanek does all the steps optimally.

输入输出样例

输入 #1
6
1 2 1 4 2
输出 #1
4
输入 #2
2
1
输出 #2
2
C++ 编辑器
输入
输出