题目描述
Problem Description
Given a tree with $n$ nodes, where the root is node $1$.
You can perform any number of Drum Stand C operations, each Drum Stand C operation involves: selecting a leaf node $x$ on the tree, then choosing an edge on the path from the root to $x$, deleting it; and then adding an edge connecting $x$ and the root.
You need to determine the maximum diameter of the tree after performing several Drum Stand C operations.
Note: A leaf node is defined as a node that is not the root and has a degree of $1$.
Input Format
This problem contains multiple test cases.
The first line contains an integer $T$, indicating the number of test cases.
For each test case, the first line contains an integer $n$, and the second line contains a sequence $f_i$ of length $n-1$, where the $i$-th element is the parent of node $i+1$ in the tree.
Output Format
For each test case, output one integer representing the answer.
Sample Input and Output
Sample Input #1
1
3
1 2
Sample Output #1
2
Sample Input #2
1
7
1 2 1 4 4 5
Sample Output #2
6
Notes
Sample #1 Explanation
Without any operations, the diameter of the tree is $2$. It can be proven that this is the maximum diameter.
Sample #2 Explanation
The tree in the sample is as follows:

Only one Drum Stand C operation is needed: select leaf node $6$, disconnect the edge from $1$ to $4$, and then connect $6$ to $1$.
This will turn the tree into a chain with a diameter of $6$. It can be proven that this is the maximum diameter.
Data Range
Please note the impact of constant factors on program efficiency.
This problem has subtask bundling and subtask dependencies enabled.
For all data, $1 \leq T \leq 10^4$, $2 \leq n \leq 2 \times 10^5$, and the input data guarantees a tree structure.
| Subtask | $T \leq$ | $n \leq$ | Special Conditions | Score | Subtask Dependencies |
|---|---|---|---|---|---|
| 1 | $10^4$ | $10$ | None | $15$ | None |
| 2 | $10^4$ | $20$ | None | $15$ | 1 |
| 3 | $10^4$ | $90$ | None | $20$ | 1,2 |
| 4 | $10^4$ | $2 \times 10^5$ | Condition $A$ | $15$ | None |
| 5 | $15$ | $2 \times 10^5$ | None | $35$ | 1,2,3,4 |
Specifically, for Subtask 4, it is guaranteed that $\sum n \leq 3 \times 10^6$.
Special Condition $A$: There does not exist a pair $(x, y)$ with $x \neq 1 \land y \neq 1$ such that $\text{LCA}(x, y) = 1$.
输入格式
见题面 Input 部分
输出格式
见题面 Output 部分