当前你的浏览器版本过低,网站已在兼容模式下运行,兼容模式仅提供最小功能支持,网站样式可能显示不正常。
请尽快升级浏览器以体验网站在线编辑、在线运行等功能。

建议使用的浏览器:

谷歌Chrome 火狐Firefox Opera浏览器 微软Edge浏览器 QQ浏览器 360浏览器 傲游浏览器

6867:Tree

题目描述
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$.
输入解释
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$.
输出解释
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.
输入样例
2
5
1 1 2 2
6
1 2 3 1 3
输出样例
17
26
来自杭电HDUOJ的附加信息
Recommend IceyWang

该题目是Virtual Judge题目,来自 杭电HDUOJ

源链接: HDU-6867

最后修改于 2020-10-25T23:35:04+00:00 由爬虫自动更新

共提交 0

通过率 --%
时间上限 内存上限
2000/1000MS(Java/Others) 65536/65536K(Java/Others)