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\)个询问, 每个询问给某个节点加上两个儿子, 并求现在树的直径大小

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

题解

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

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

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

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

Codeforces 379F New Year Tree的更多相关文章

  1. Codeforces 379F New Year Tree 树的直径的性质推理

    New Year Tree 我们假设当前的直径两端为A, B, 那么现在加入v的两个儿子x, y. 求直径的话我们可以第一次dfs找到最远点这个点必定为直径上的点, 然而用这个点第二次dfs找到最远点 ...

  2. Codeforces 461B Appleman and Tree(木dp)

    题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...

  3. Codeforces 1129 E.Legendary Tree

    Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1​\) 次 \((S=\{1\},T=\{ ...

  4. Codeforces 280C Game on tree【概率DP】

    Codeforces 280C Game on tree LINK 题目大意:给你一棵树,1号节点是根,每次等概率选择没有被染黑的一个节点染黑其所有子树中的节点,问染黑所有节点的期望次数 #inclu ...

  5. Codeforces A. Game on Tree(期望dfs)

    题目描述: Game on Tree time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  6. Codeforces Round #781(C. Tree Infection)

    Codeforces Round #781 C. Tree Infection time limit per test 1 second memory limit per test 256 megab ...

  7. CodeForces 379F 树的直径 New Year Tree

    题意:每次操作新加两个叶子节点,每次操作完以后询问树的直径. 维护树的直径的两个端点U,V,每次计算一下新加进来的叶子节点到U,V两点的距离,如果有更长的就更新. 因为根据树的直径的求法,若出现新的直 ...

  8. Codeforces 734E. Anton and Tree 搜索

    E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...

  9. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...

随机推荐

  1. IDEA rider 管道模式 经典模式(2)

    1.这里设置为Classic,并打开applicationhost.config将对应应用的 Clr4IntegratedAppPool全部替换为 Clr4ClassicAppPool 2.Confi ...

  2. WebSocket的简单认识&SpringBoot整合websocket

    1. 什么是WebSocket?菜鸟对websocket的解释如下 WebSocket 是 HTML5 开始提供的一种在单个 TCP 连接上进行全双工通讯的协议. WebSocket 使得客户端和服务 ...

  3. 一个简单的 ValueTask 的示例

    Task 确实有潜在的缺点,特别是对于实例创建很多  并且高吞吐量和性能是关键问题的场景  :  Task 是一个类.作为一个类,这意味着任何需要创建一个对象的操作都需要分配一个对象,分配的对象越多, ...

  4. 【06】Kubernets:资源清单(控制器 - Deployment)

    写在前面的话 上一节主要简单的提了一下控制器都有哪些常用的,并且简单的功能是啥,最后一并提了 ReplicaSet 控制器. 但是 ReplicaSet 一般不需要我们直接配置,多以从本节开始,开始学 ...

  5. 单片机成长之路(stm8基础篇)- 025 stm8 时钟切换

    stm8 时钟切换; /************************************ 时钟设置 ************************************/ // 时钟 0: ...

  6. WPF 精修篇 非UI进程后台更新UI进程

    原文:WPF 精修篇 非UI进程后台更新UI进程 <Grid> <Grid.RowDefinitions> <RowDefinition Height="11* ...

  7. 2019 海看java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.海看等公司offer,岗位是Java后端开发,因为发展原因最终选择去了海看,入职一年时间了,也成为了面试官,之 ...

  8. JavaScript之二十三种设计模式

    23种JavaScript设计模式   原文链接:https://boostlog.io/@sonuton/23-javascript-design-patterns-5adb006847018500 ...

  9. 正则表达式,匹配非本站图片网址去掉img标签内容实例

    正则表达式,匹配非本站图片网址去掉img标签内容实例 在线正则表达式测试http://tool.oschina.net/regex/# 测试内容: <div><p>eee< ...

  10. 点击Button按钮实现页面跳转

    1.首先我们新建一个带有button按钮的页面 <button type="submit" class="form-contrpl">注册</ ...