CF1856E2.PermuTree (hard version)

省选/NOI-

通过率:0%

时间限制:3.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

This is the hard version of the problem. The differences between the two versions are the constraint on nn and the time limit. You can make hacks only if both versions of the problem are solved.

You are given a tree with nn vertices rooted at vertex 11.

For some permutation†^\dagger aa of length nn, let f(a)f(a) be the number of pairs of vertices (u,v)(u, v) such that au<alca⁡(u,v)<ava_u \lt a_{\operatorname{lca}(u, v)} \lt a_v. Here, lca⁡(u,v)\operatorname{lca}(u,v) denotes the lowest common ancestor of vertices uu and vv.

Find the maximum possible value of f(a)f(a) over all permutations aa of length nn.

†^\dagger A permutation of length nn is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array), and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

这是该问题的困难版本。两个版本之间的区别在于对 nn 的约束以及时间限制。只有当该问题的两个版本均被解决时,你才可以进行 hack。

你被给定一棵以顶点 11 为根、包含 nn 个顶点的树。

对于某个长度为 nn 的排列†^\dagger aa,定义 f(a)f(a) 为满足 au<alca⁡(u,v)<ava_u \lt a_{\operatorname{lca}(u, v)} \lt a_v 的顶点对 (u,v)(u, v) 的数量。其中,lca⁡(u,v)\operatorname{lca}(u,v) 表示顶点 uu 和 vv 的最近公共祖先。

求所有长度为 nn 的排列 aa 中,f(a)f(a) 的最大可能值。

†^\dagger 长度为 nn 的排列是指由 11 到 nn 中互不相同的 nn 个整数按任意顺序组成的数组。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列,但 [1,2,2][1,2,2] 不是排列(数字 22 在数组中出现了两次),[1,3,4][1,3,4] 也不是排列(此时 n=3n=3,但数组中出现了 44)。

输入格式

The first line contains a single integer nn (2≤n≤1062 \le n \le 10^6).

The second line contains n−1n - 1 integers p2,p3,…,pnp_2,p_3,\ldots,p_n (1≤pi<i1 \le p_i \lt i) indicating that there is an edge between vertices ii and pip_i.

第一行包含一个整数 nn(2≤n≤1062 \le n \le 10^6)。

第二行包含 n−1n - 1 个整数 p2,p3,…,pnp_2, p_3, \ldots, p_n(1≤pi<i1 \le p_i < i),表示顶点 ii 与顶点 pip_i 之间存在一条边。

输出格式

Output the maximum value of f(a)f(a).

输出 f(a)f(a) 的最大值。

输入输出样例

  • 输入#1

    5
    1 1 3 3

    输出#1

    4
  • 输入#2

    2
    1

    输出#2

    0
  • 输入#3

    6
    1 2 2 1 5

    输出#3

    7
  • 输入#4

    4
    1 1 1

    输出#4

    2

说明/提示

The tree in the first test:

One possible optimal permutation aa is [2,1,4,5,3][2, 1, 4, 5, 3] with 44 suitable pairs of vertices:

  • (2,3)(2, 3), since lca⁡(2,3)=1\operatorname{lca}(2, 3) = 1 and 1<2<41 \lt 2 \lt 4,
  • (2,4)(2, 4), since lca⁡(2,4)=1\operatorname{lca}(2, 4) = 1 and 1<2<51 \lt 2 \lt 5,
  • (2,5)(2, 5), since lca⁡(2,5)=1\operatorname{lca}(2, 5) = 1 and 1<2<31 \lt 2 \lt 3,
  • (5,4)(5, 4), since lca⁡(5,4)=3\operatorname{lca}(5, 4) = 3 and 3<4<53 \lt 4 \lt 5.

The tree in the third test:

The tree in the fourth test:

第一个测试用例中的树:

一种可能的最优排列 aa 为 [2,1,4,5,3][2, 1, 4, 5, 3],包含 44 对满足条件的顶点:

  • (2,3)(2, 3),因为 lca⁡(2,3)=1\operatorname{lca}(2, 3) = 1 且 1<2<41 \lt 2 \lt 4,
  • (2,4)(2, 4),因为 lca⁡(2,4)=1\operatorname{lca}(2, 4) = 1 且 1<2<51 \lt 2 \lt 5,
  • (2,5)(2, 5),因为 lca⁡(2,5)=1\operatorname{lca}(2, 5) = 1 且 1<2<31 \lt 2 \lt 3,
  • (5,4)(5, 4),因为 lca⁡(5,4)=3\operatorname{lca}(5, 4) = 3 且 3<4<53 \lt 4 \lt 5。

第三个测试用例中的树:

第四个测试用例中的树:

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

首页