CF1662G.Gastronomic Event

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

SWERC organizers want to hold a gastronomic event.

The location of the event is a building with nn rooms connected by n−1n-1 corridors (each corridor connects two rooms) so that it is possible to go from any room to any other room.

In each room you have to set up the tasting of a typical Italian dish. You can choose from nn typical Italian dishes rated from 11 to nn depending on how good they are (nn is the best possible rating). The nn dishes have distinct ratings.

You want to assign the nn dishes to the nn rooms so that the number of pleasing tours is maximal. A pleasing tour is a nonempty sequence of rooms so that:

  • Each room in the sequence is connected to the next one in the sequence by a corridor.
  • The ratings of the dishes in the rooms (in the order given by the sequence) are increasing.

If you assign the nn dishes optimally, what is the maximum number of pleasing tours?

SWERC 组委会希望举办一场美食活动。

活动地点是一座拥有 nn 个房间的建筑,这些房间由 n−1n-1 条走廊连接(每条走廊连接两个房间),且任意两个房间之间均可相互到达。

每个房间需布置一道典型的意大利菜肴供品鉴。你可以从 nn 道典型意大利菜肴中选择,这些菜肴按品质被评分为 11 到 nn(nn 为最高评分)。这 nn 道菜肴的评分互不相同。

你需要将这 nn 道菜肴分配到 nn 个房间中,使得“令人愉悦的游览路线”(pleasing tour)的数量最大化。所谓“令人愉悦的游览路线”,是指一个非空的房间序列,满足:

  • 序列中每个房间均通过一条走廊与序列中的下一个房间相连;
  • 按该序列顺序,各房间所布置菜肴的评分严格递增。

若以最优方式分配这 nn 道菜肴,最多能有多少条“令人愉悦的游览路线”?

输入格式

The first line contains an integer nn (2≤n≤1 000 0002\le n\le 1\,000\,000) — the number of rooms.

The second line contains n−1n-1 integers p2,p3,⋯ ,pnp_2, p_3, \cdots , p_n (1≤pi<i1 \leq p_i \lt i). Each pip_i indicates that there is a corridor between room ii and room pip_i. It is guaranteed that the building has the property that it is possible to go from any room to any other room.

第一行包含一个整数 nn(2≤n≤1 000 0002\le n\le 1\,000\,000)—— 房间的数量。

第二行包含 n−1n-1 个整数 p2,p3,⋯ ,pnp_2, p_3, \cdots , p_n(1≤pi<i1 \leq p_i \lt i)。每个 pip_i 表示房间 ii 与房间 pip_i 之间存在一条走廊。保证该建筑具有如下性质:任意两个房间之间均可相互到达。

输出格式

Print the maximum number of pleasing tours.

输出令人愉悦的游览路线的最大数量。

输入输出样例

  • 输入#1

    5
    1 2 2 2

    输出#1

    13
  • 输入#2

    10
    1 2 3 4 3 2 7 8 7

    输出#2

    47

说明/提示

In the first sample, it is optimal to place the dish with rating 11 in room 11, the dish with rating 22 in room 33, the dish with rating 33 in room 22, the dish with rating 44 in room 55 and the dish with rating 55 in room 44.

All the 1313 possible pleasing tours are: (1)(1), (2)(2), (3)(3), (4)(4), (5)(5), (1,2)(1,2), (3,2)(3,2), (2,4)(2,4), (2,5)(2,5), (1,2,4)(1,2,4), (1,2,5)(1,2,5), (3,2,4)(3,2,4), (3,2,5)(3,2,5).

There are also other ways to assign the dishes to the rooms so that there are 1313 pleasing tours.

在第一个样例中,最优方案是将评分为 11 的菜肴放在房间 11,评分为 22 的菜肴放在房间 33,评分为 33 的菜肴放在房间 22,评分为 44 的菜肴放在房间 55,评分为 55 的菜肴放在房间 44。

所有 1313 种可能的愉悦游线为:(1)(1)、(2)(2)、(3)(3)、(4)(4)、(5)(5)、(1,2)(1,2)、(3,2)(3,2)、(2,4)(2,4)、(2,5)(2,5)、(1,2,4)(1,2,4)、(1,2,5)(1,2,5)、(3,2,4)(3,2,4)、(3,2,5)(3,2,5)。

此外,还存在其他将菜肴分配至房间的方式,也能得到 1313 条愉悦游线。

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

首页