CF1740E.Hanging Hearts

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Pak Chanek has nn blank heart-shaped cards. Card 11 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 ii (i>1i \gt 1) is hanging onto card pip_i (pi<ip_i \lt i).

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

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

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

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

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

帕克·查内克有 nn 张空白的心形卡片。第 11 张卡片直接贴在墙上,其余每张卡片均通过一根细绳恰好悬挂在另一张卡片上。具体而言,第 ii 张卡片(i>1i > 1)悬挂在第 pip_i 张卡片上(满足 pi<ip_i < i)。

最初,帕克·查内克必须在每张卡片上写一个整数。他通过选择 [1,2,…,n][1, 2, \dots, n] 的任意一个排列 aa 来完成此操作;然后,第 ii 张卡片上所写的数字即为 aia_i。

随后,帕克·查内克需执行以下操作共 nn 次,同时维护一个序列 ss(初始为空):

  1. 选择一张卡片 xx,使得没有其他卡片悬挂在它上面;
  2. 将卡片 xx 上所写的数字添加到序列 ss 的末尾;
  3. 若 x≠1x \neq 1 且卡片 pxp_x 上的数字大于卡片 xx 上的数字,则将卡片 pxp_x 上的数字替换为卡片 xx 上的数字;
  4. 移除卡片 xx。

最终,帕克·查内克将得到一个长度为 nn 的序列 ss。若帕克·查内克在整个过程中均采取最优策略,那么 ss 的最长非递减子序列†^\dagger 的最大可能长度是多少?

†^\dagger 序列 bb 是序列 cc 的一个子序列,当且仅当 bb 可通过从 cc 中删除若干(可能为零个或全部)元素而得到。例如,[3,1][3,1] 是 [3,2,1][3,2,1]、[4,3,1][4,3,1] 和 [3,1][3,1] 的子序列,但不是 [1,3,3,7][1,3,3,7] 和 [3,10,4][3,10,4] 的子序列。

输入格式

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

The second line contains n−1n - 1 integers p2,p3,…,pnp_2, p_3, \dots, p_n (1≤pi<i1 \le p_i \lt i) describing which card that each card hangs onto.

第一行包含一个整数 nn(2≤n≤1052 \le n \le 10^5)—— 心形卡片的数量。

第二行包含 n−1n - 1 个整数 p2,p3,…,pnp_2, p_3, \dots, p_n(1≤pi<i1 \le p_i \lt i),描述每张卡片所悬挂于的卡片编号。

输出格式

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

输出一个整数——如果 Pak Chanek 以最优方式执行所有步骤,字符串 ss 的最长非递减子序列的最大长度。

输入输出样例

  • 输入#1

    6
    1 2 1 4 2

    输出#1

    4
  • 输入#2

    2
    1

    输出#2

    2

说明/提示

The following is the structure of the cards in the first example.

Pak Chanek can choose the permutation a=[1,5,4,3,2,6]a = [1, 5, 4, 3, 2, 6].

Let wiw_i be the number written on card ii. Initially, wi=aiw_i = a_i. Pak Chanek can do the following operations in order:

  1. Select card 55. Append w5=2w_5 = 2 to the end of ss. As w4>w5w_4 \gt w_5, the value of w4w_4 becomes 22. Remove card 55. After this operation, s=[2]s = [2].
  2. Select card 66. Append w6=6w_6 = 6 to the end of ss. As w2≤w6w_2 \leq w_6, the value of w2w_2 is left unchanged. Remove card 66. After this operation, s=[2,6]s = [2, 6].
  3. Select card 44. Append w4=2w_4 = 2 to the end of ss. As w1≤w4w_1 \leq w_4, the value of w1w_1 is left unchanged. Remove card 44. After this operation, s=[2,6,2]s = [2, 6, 2].
  4. Select card 33. Append w3=4w_3 = 4 to the end of ss. As w2>w3w_2 \gt w_3, the value of w2w_2 becomes 44. Remove card 33. After this operation, s=[2,6,2,4]s = [2, 6, 2, 4].
  5. Select card 22. Append w2=4w_2 = 4 to the end of ss. As w1≤w2w_1 \leq w_2, the value of w1w_1 is left unchanged. Remove card 22. After this operation, s=[2,6,2,4,4]s = [2, 6, 2, 4, 4].
  6. Select card 11. Append w1=1w_1 = 1 to the end of ss. Remove card 11. After this operation, s=[2,6,2,4,4,1]s = [2, 6, 2, 4, 4, 1].

One of the longest non-decreasing subsequences of s=[2,6,2,4,4,1]s = [2, 6, 2, 4, 4, 1] is [2,2,4,4][2, 2, 4, 4]. Thus, the length of the longest non-decreasing subsequence of ss is 44. It can be proven that this is indeed the maximum possible length.

以下是第一个示例中卡片的结构。

Pak Chanek 可以选择排列 a=[1,5,4,3,2,6]a = [1, 5, 4, 3, 2, 6]。

令 wiw_i 表示第 ii 张卡片上所写的数字。初始时,wi=aiw_i = a_i。Pak Chanek 可按如下顺序执行以下操作:

  1. 选择第 55 张卡片。将 w5=2w_5 = 2 追加到序列 ss 的末尾。由于 w4>w5w_4 \gt w_5,w4w_4 的值变为 22。移除第 55 张卡片。该操作后,s=[2]s = [2]。
  2. 选择第 66 张卡片。将 w6=6w_6 = 6 追加到序列 ss 的末尾。由于 w2≤w6w_2 \leq w_6,w2w_2 的值保持不变。移除第 66 张卡片。该操作后,s=[2,6]s = [2, 6]。
  3. 选择第 44 张卡片。将 w4=2w_4 = 2 追加到序列 ss 的末尾。由于 w1≤w4w_1 \leq w_4,w1w_1 的值保持不变。移除第 44 张卡片。该操作后,s=[2,6,2]s = [2, 6, 2]。
  4. 选择第 33 张卡片。将 w3=4w_3 = 4 追加到序列 ss 的末尾。由于 w2>w3w_2 \gt w_3,w2w_2 的值变为 44。移除第 33 张卡片。该操作后,s=[2,6,2,4]s = [2, 6, 2, 4]。
  5. 选择第 22 张卡片。将 w2=4w_2 = 4 追加到序列 ss 的末尾。由于 w1≤w2w_1 \leq w_2,w1w_1 的值保持不变。移除第 22 张卡片。该操作后,s=[2,6,2,4,4]s = [2, 6, 2, 4, 4]。
  6. 选择第 11 张卡片。将 w1=1w_1 = 1 追加到序列 ss 的末尾。移除第 11 张卡片。该操作后,s=[2,6,2,4,4,1]s = [2, 6, 2, 4, 4, 1]。

序列 s=[2,6,2,4,4,1]s = [2, 6, 2, 4, 4, 1] 的一个最长非递减子序列为 [2,2,4,4][2, 2, 4, 4]。因此,ss 的最长非递减子序列的长度为 44。可以证明,这确实是可能达到的最大长度。

输入解题思路,AI测评打分。不知道怎么写?

首页