zoukankan      html  css  js  c++  java
  • Codeforces 379F New Year Tree

    F. New Year Tree

    time limit per test2 seconds
    memory limit per test256 megabytes

    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!

    Input

    The first line contains integer (q (1 ≤ q ≤ 5·10^5)) — 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.

    Output

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

    Examples

    input

    5
    2
    3
    4
    8
    5

    output

    3
    4
    4
    5
    6

    题意描述

    一开始有一个根为1的树,他有三个儿子2,3,4,树的边权都为1

    现在有(q)个询问, 每个询问给某个节点加上两个儿子, 并求现在树的直径大小

    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .
    .

    题解

    如果说新加的点能使直径变长, 那么他一定接在某一条直径的端点上,

    考虑直径可能有多条,但是所有直径一定有公共部分,且公共部分以外的路径也一定等长

    所以一个新加的点增长一条直径, 他就能增长所有直径

    所以记录随便一条直径的两端点即可

  • 相关阅读:
    360手机青春版原装充电器
    然并卵却很拉风!360超级充电器评测_天极网
    39元超值!360超级充电器拆解与评测
    99元紫米10000mAh移动电源开箱拆解
    自己DIY PCB电路板制作(简单方便快捷)
    Easy EDA:在线创客软件服务是未来的趋势 | 雷锋网
    EasyEDA
    我的那些年(9)~我来团队了,Mvc兴起了
    我的那些年(8)~去朋友公司做网站开发
    我的那些年(7)~第一份互联网工作
  • 原文地址:https://www.cnblogs.com/Kuonji/p/10385121.html
Copyright © 2011-2022 走看看