7589: Tree

内存限制:128 MB 时间限制:1 S
题面:传统 评测方式:文本比较 上传者:
提交:16 通过:11

题目描述

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 pi . 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 (1T100000) — the number of test cases.

The first line of each test case contains only one integer n(1n5×105) — the number of vertices in the tree.

The second line of each test case contains n1 integers p2,p3,,pn(1pi<i) — the parent of each non-root node.

The sum of n over all test cases does not exceed 106 .

输出格式

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