CF1856E1.PermuTree (easy version)
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
This is the easy version of the problem. The differences between the two versions are the constraint on n and the time limit. You can make hacks only if both versions of the problem are solved.
You are given a tree with n vertices rooted at vertex 1 .
For some permutation † a of length n , let f(a) be the number of pairs of vertices (u,v) such that au<alca(u,v)<av . Here, lca(u,v) denotes the lowest common ancestor of vertices u and v .
Find the maximum possible value of f(a) over all permutations a of length n .
† A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation ( 2 appears twice in the array), and [1,3,4] is also not a permutation ( n=3 but there is 4 in the array).
输入格式
The first line contains a single integer n ( 2≤n≤5000 ).
The second line contains n−1 integers p2,p3,…,pn ( 1≤pi<i ) indicating that there is an edge between vertices i and pi .
输出格式
Output the maximum value of 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 a is [2,1,4,5,3] with 4 suitable pairs of vertices:
- (2,3) , since lca(2,3)=1 and 1<2<4 ,
- (2,4) , since lca(2,4)=1 and 1<2<5 ,
- (2,5) , since lca(2,5)=1 and 1<2<3 ,
- (5,4) , since lca(5,4)=3 and 3<4<5 .
The tree in the third test:
The tree in the fourth test: