Codeforces Round 873 (Div. 2)
F
Two Centroids
Codeforces
未评级
原题
← E→
翻译中…

You are given a tree (an undirected connected acyclic graph) which initially only contains vertex $1$. There will be several queries to the given tree. In the $i$-th query, vertex $i + 1$ will appear and be connected to vertex $p_i$ ($1 \le p_i \le i$).

翻译中…

After each query, please find out the least number of operations required to make the current tree has two centroids. In one operation, you can add one vertex and one edge to the tree such that it remains a tree.

翻译中…

A vertex is called a centroid if its removal splits the tree into subtrees with at most $\lfloor \frac{n}{2} \rfloor$ vertices each, with $n$ as the number of vertices of the tree. For example, the centroid of the following tree is $3$ because the biggest subtree after removing the centroid has $2$ vertices.

翻译中…

image

翻译中…

In the next tree, vertex $1$ and $2$ are both centroids.

翻译中…

image

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains a single integer $n$ ($2 \le n \le 5 \cdot 10^{5}$) — the number of nodes of the final tree.

翻译中…

The second line of each test case contains $n - 1$ integers $p_1, p_2, \ldots, p_{n - 1}$ ($1 \le p_i \le i$) — the index of the vertex that is connected to vertex $i + 1$.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^{5}$.

翻译中…

Output

翻译中…

For each test case, output $n - 1$ integers. The $i$-th integer is the answer to the $i$-th query — the least number of operations required to make the current tree have two centroids.

翻译中…

We can show that an answer always exists.

翻译中…

Note

翻译中…

The illustrations below are of the fourth example test case.

翻译中…

After the third query:

翻译中…

image

翻译中…

The tree already has vertices $2$ and $3$ as centroids, so no operations are needed.

翻译中…

After the fourth query:

翻译中…

image

翻译中…

Adding vertex $x$ to the tree makes vertices $2$ and $3$ centroids. Only one operation is needed.

翻译中…

After the fifth query:

翻译中…

image

翻译中…

Adding vertex $x$ and $y$ to the tree makes vertices $5$ and $2$ centroids. Two operations are needed.

翻译中…

After the sixth query:

翻译中…

image

翻译中…

Adding vertex $x$, $y$, and $z$ to the tree makes vertices $5$ and $2$ centroids. Three operations are needed.

翻译中…

样例

Input #1
5
2
1
3
1 1
4
1 2 3
7
1 2 3 2 5 2
10
1 2 2 4 5 5 7 8 9
Output #1
0
0 1
0 1 0
0 1 0 1 2 3
0 1 2 1 0 1 0 1 2
InputCopy OutputCopy
题目信息
题号
1828F
时间限制
1.5 s
内存限制
1024 MB

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。