QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 512 MB Total points: 100

#1246. Family photo

الإحصائيات

Bitian 家族正在举行家庭聚会。家族成员的编号为从 $1$ 到 $n$ 的连续整数。Bityslav 是家族中最年长的成员,编号为 $1$。其余每个家族成员都有且仅有一个父辈(虽然有点奇怪,但事实就是这样)。

这场精彩的家庭聚会需要被记录下来,因此需要拍摄一张照片。这张照片将展示排成一排的部分家族成员。然而,今年的家族成员非常挑剔。两名成员同意站在一起的充要条件是:其中一人是另一人的祖先(不一定是直接父辈)$^2$。

Bityslav 现在面临一个艰巨的任务,他希望尽可能多地让家族成员出现在照片中。请问照片中最多能容纳多少人?

输入格式

第一行包含一个整数 $n$ ($2 \le n \le 300\,000$),表示 Bitian 家族成员的数量。 第二行包含 $n-1$ 个整数 $p_2, p_3, \dots, p_n$ ($1 \le p_i \le n, p_i \neq i$ 对于 $2 \le i \le n$),其中 $p_i$ 表示第 $i$ 个家族成员的父辈编号。你可以假设这个家谱树中不存在环,即没有人是自己的祖先。

输出格式

输出一个整数,表示在满足家族成员限制条件的情况下,照片中能够容纳的最大人数。

样例

输入 1

8
5 5 5 1 1 6 7

输出 1

7

说明 1

下图展示了 Bitian 家族的家谱树。其中一种最优的拍照方案是按以下顺序排列成员:7, 6, 8, 1, 2, 5, 3。可以证明,让所有成员出现在同一张照片中是不可能的。

家谱树结构

$^2$ 我们称 A 是 B 的祖先,当且仅当 A 是 B 的父辈,或者 A 是 B 的某位祖先的父辈。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.