

TreeTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1153 Accepted Submission(s): 446 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 $n1$ integers $p_2,p_3,\dots,p_n (1\leq p_i<i)$ ¡ª the parent of each nonroot 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. Sample Input
Sample Output
Source  
