CF1827D.Two Centroids

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a tree (an undirected connected acyclic graph) which initially only contains vertex 11 . There will be several queries to the given tree. In the ii -th query, vertex i+1i + 1 will appear and be connected to vertex pip_i ( 1pii1 \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 n2\lfloor \frac{n}{2} \rfloor vertices each, with nn as the number of vertices of the tree. For example, the centroid of the following tree is 33 because the biggest subtree after removing the centroid has 22 vertices.

In the next tree, vertex 11 and 22 are both centroids.

输入格式

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

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

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

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

输出格式

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

We can show that an answer always exists.

输入输出样例

  • 输入#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

    输出#1

    0
    0 1
    0 1 0
    0 1 0 1 2 3
    0 1 2 1 0 1 0 1 2

说明/提示

The illustrations below are of the fourth example test case.

After the third query:

The tree already has vertices 22 and 33 as centroids, so no operations are needed.After the fourth query:

Adding vertex xx to the tree makes vertices 22 and 33 centroids. Only one operation is needed.After the fifth query:

Adding vertex xx and yy to the tree makes vertices 55 and 22 centroids. Two operations are needed.After the sixth query:

Adding vertex xx , yy , and zz to the tree makes vertices 55 and 22 centroids. Three operations are needed.

首页