CF379F.New Year Tree

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are a programmer and you have a New Year Tree (not the traditional fur tree, though) — a tree of four vertices: one vertex of degree three (has number 1), connected with three leaves (their numbers are from 2 to 4).

On the New Year, programmers usually have fun. You decided to have fun as well by adding vertices to the tree. One adding operation looks as follows:

  • First we choose some leaf of the tree with number v.
  • Let's mark the number of vertices on the tree at this moment by variable n, then two vertexes are added to the tree, their numbers are n + 1 and n + 2, also you get new edges, one between vertices v and n + 1 and one between vertices v and n + 2.

Your task is not just to model the process of adding vertices to the tree, but after each adding operation print the diameter of the current tree. Come on, let's solve the New Year problem!

你是一名程序员,拥有一棵“新年树”(虽然它并非传统的松树)——一棵包含四个顶点的树:其中一个顶点度数为三(编号为 1),与三个叶子节点相连(编号分别为 2、3、4)。

在新年期间,程序员们通常会玩得开心。你也决定通过向这棵树中添加顶点来获得乐趣。每次添加操作如下:

  • 首先选择当前树中的某个叶子节点,其编号为 vv;
  • 设此时树中已有 nn 个顶点,则向树中新增两个顶点,编号分别为 n+1n+1 和 n+2n+2;同时新增两条边:一条连接顶点 vv 与 n+1n+1,另一条连接顶点 vv 与 n+2n+2。

你的任务不仅是要模拟向树中添加顶点的过程,而且每次添加操作后,都要输出当前树的直径。来吧,一起解决这个新年问题!

输入格式

The first line contains integer q (1 ≤ q ≤ 5·105) — the number of operations. Each of the next q lines contains integer v__i (1 ≤ v__i ≤ n) — the operation of adding leaves to vertex v__i. Variable n represents the number of vertices in the current tree.

It is guaranteed that all given operations are correct.

第一行包含一个整数 qq(1≤q≤5⋅1051 \le q \le 5 \cdot 10^5)—— 操作的次数。接下来的 qq 行中,每行包含一个整数 viv_i(1≤vi≤n1 \le v_i \le n)—— 表示向顶点 viv_i 添加叶子节点的操作。变量 nn 表示当前树中的顶点数量。

保证所有给定的操作均合法。

输出格式

Print q integers — the diameter of the current tree after each operation.

输出 q 个整数——每次操作后当前树的直径。

输入输出样例

  • 输入#1

    5
    2
    3
    4
    8
    5

    输出#1

    3
    4
    4
    5
    6

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

首页