博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces 379F New Year Tree
阅读量:5314 次
发布时间:2019-06-14

本文共 1711 字,大约阅读时间需要 5 分钟。

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

你可能感兴趣的文章
Henan Hongxing broken equipment in practice for testing truth
查看>>
【Linux】【Services】【nfs】nfs安装与配置
查看>>
PHP json_encode() 函数介绍
查看>>
android wifi
查看>>
P2680 运输计划
查看>>
运输层
查看>>
cudaMalloc和cudaMallocPitch
查看>>
如何打卡后缀为3ds的文件
查看>>
POJ2524 并查集
查看>>
boost asio resolver
查看>>
<转>.h和.cpp文件的区别
查看>>
[转]svn常用命令
查看>>
Swing学习1——总体概述
查看>>
nginx 注释配置及详解
查看>>
QCustomplot(一) 能做什么事
查看>>
vue1.0和vue2.0生命周期----整理一
查看>>
Could not load the Tomcat server configuration at \Servers\Tomcat v7.0 Server at localhost-config
查看>>
对象的成员的初始化
查看>>
zbb20180710 maven Failed to read artifact descriptor--maven
查看>>
关于Webapp的注意事项
查看>>