CF1827D.Two Centroids

省选/NOI-

通过率:0%

时间限制:1.50s

内存限制:1024MB

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 (1≤pi≤i1 \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.

给你一棵树(一个无向、连通且无环的图),该树初始时仅包含顶点 11。接下来将对该树进行若干次查询。在第 ii 次查询中,顶点 i+1i + 1 将被加入,并与顶点 pip_i 相连(其中 1≤pi≤i1 \le p_i \le i)。

每次查询后,请计算使当前树恰好拥有两个重心所需的最少操作次数。每次操作允许你向树中添加一个顶点和一条边,且操作后图仍需保持为一棵树。

若删除某顶点后,所得各连通子树的大小均不超过 ⌊n2⌋\lfloor \frac{n}{2} \rfloor(其中 nn 为树的顶点总数),则称该顶点为重心。例如,下图中顶点 33 是重心,因为删去它后产生的最大子树含 22 个顶点。

在下一棵树中,顶点 11 和 22 均为重心。

输入格式

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

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

The second line of each test case contains n−1n - 1 integers p1,p2,…,pn−1p_1, p_2, \ldots, p_{n - 1} (1≤pi≤i1 \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 5⋅1055 \cdot 10^{5}.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤5⋅1052 \le n \le 5 \cdot 10^{5})——表示最终树的节点数。

每个测试用例的第二行包含 n−1n - 1 个整数 p1,p2,…,pn−1p_1, p_2, \ldots, p_{n - 1}(1≤pi≤i1 \le p_i \le i)——表示与节点 i+1i + 1 相连的节点编号。

保证所有测试用例的 nn 之和不超过 5⋅1055 \cdot 10^{5}。

输出格式

For each test case, output n−1n - 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.

对于每个测试用例,输出 n−1n - 1 个整数。其中第 ii 个整数是第 ii 个查询的答案——即令当前树具有两个重心所需的最少操作次数。

可以证明答案一定存在。

输入输出样例

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

以下插图展示的是第四个示例测试用例。

第三次查询后:

此时树中顶点 22 和 33 已是重心,因此无需执行任何操作。

第四次查询后:

向树中添加顶点 xx 后,顶点 22 和 33 成为重心。仅需执行一次操作。

第五次查询后:

向树中添加顶点 xx 和 yy 后,顶点 55 和 22 成为重心。需要执行两次操作。

第六次查询后:

向树中添加顶点 xx、yy 和 zz 后,顶点 55 和 22 成为重心。需要执行三次操作。

输入解题思路,AI测评打分。不知道怎么写?

首页