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

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

题解

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

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

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

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

转载于:https://www.cnblogs.com/ikihsiguoyr/p/10385121.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值