#P10794. 『SpOI - R1』架子鼓可以站 C
『SpOI - R1』架子鼓可以站 C
Problem Description
You are given a tree with nodes, where the root is node .
You can perform the following “Stand C operation” any number of times. In one “Stand C operation”:
Choose a leaf node in the tree, then choose an edge on the path from the root to and delete it; after that, add an edge connecting and the root.
You need to find the maximum possible diameter of the tree after performing some number of “Stand C operations”.
Note: A leaf node is defined as a node that is not the root and has degree .
Input Format
This problem contains multiple test cases.
The first line contains an integer , the number of test cases.
Then follow test cases. For each test case, the first line contains an integer . The second line contains a sequence of length , where the -th value is the parent of node in the tree.
Output Format
For each test case, output one integer per line, the answer.
1
3
1 2
2
1
7
1 2 1 4 4 5
6
Hint
Explanation of Sample #1
If no operation is performed, the diameter of the tree is . It can be proven that this is the maximum diameter.
Explanation of Sample #2
The tree in the sample is as follows:

Only one “Stand C operation” is needed: choose leaf node , disconnect the edge from to , then connect and .
This will make the tree become a chain, with diameter . It can be proven that this is the maximum diameter.
Constraints
Please pay attention to how constant factors affect program efficiency.
This problem enables subtask bundling and subtask dependencies.
For all testdata, , , and the input is guaranteed to form a tree.
| Subtask | Special property | Score | Dependency | ||
|---|---|---|---|---|---|
| 1 | None | None | |||
| 2 | 1 | ||||
| 3 | 1,2 | ||||
| 4 | None | ||||
| 5 | None | 1,2,3,4 |
In particular, for Subtask 4, it is guaranteed that .
Special property : There does not exist a pair with such that .
Translated by ChatGPT 5