Tree

TimeLimit:1000MS  MemoryLimit:65536KB
64-bit integer IO format:%I64d
未提交 | 登录后收藏
Problem Description
You are given a tree consisting of $n$ vertices numbered $1$ to $n$ rooted at node $1$. The parent of the $i$-th vertices is $p_i$. You can move from a vertex to any of its children. What's more, you can add one directed edge between any two different vertices, and you can move through this edge too. You need to maximize the number of pairs $(x,y)$ such that $x$ can move to $y$ through the edges after adding the edge. Note that $x$ can also move to $x$.
Input
The first line contains one integer $T$ $(1\leq T\leq 100000)$ — the number of test cases.

The first line of each test case contains only one integer $n(1\leq n\leq 5\times 10^5)$ — the number of vertices in the tree.

The second line of each test case contains $n-1$ integers $p_2,p_3,\dots,p_n (1\leq p_i<i)$ — the parent of each non-root node.

The sum of $n$ over all test cases does not exceed $10^6$.
Output
Print $T$ integers — for each test case output the maximum number of pairs $(x,y)$ that vertices $x$ can move to $y$ after adding one edge.
SampleInput
2
5
1 1 2 2
6
1 2 3 1 3
SampleOutput
17
26
Submit
题目统计信息详细
总AC数4
通过人数3
尝试人数3
总提交量12
AC率25.00%
AC该题后可以添加标签
贴完标签可以获得20ACB。
并且可以获得本题所有提交代码查看权限。
点击标题可以显示标签。
如果你还没认真思考过这题,请不要查看标签
如果您已经通过了该题,请务为该题贴上标签

T^T Online Judge

[BUG反馈] [FAQ] [闽ICP备17026590号-1]
当前版本:3.24 系统时间: