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\)个询问, 每个询问给某个节点加上两个儿子, 并求现在树的直径大小
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
题解
如果说新加的点能使直径变长, 那么他一定接在某一条直径的端点上,
考虑直径可能有多条,但是所有直径一定有公共部分,且公共部分以外的路径也一定等长
所以一个新加的点增长一条直径, 他就能增长所有直径
所以记录随便一条直径的两端点即可
Codeforces 379F New Year Tree的更多相关文章
- Codeforces 379F New Year Tree 树的直径的性质推理
New Year Tree 我们假设当前的直径两端为A, B, 那么现在加入v的两个儿子x, y. 求直径的话我们可以第一次dfs找到最远点这个点必定为直径上的点, 然而用这个点第二次dfs找到最远点 ...
- Codeforces 461B Appleman and Tree(木dp)
题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...
- Codeforces 1129 E.Legendary Tree
Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1\) 次 \((S=\{1\},T=\{ ...
- Codeforces 280C Game on tree【概率DP】
Codeforces 280C Game on tree LINK 题目大意:给你一棵树,1号节点是根,每次等概率选择没有被染黑的一个节点染黑其所有子树中的节点,问染黑所有节点的期望次数 #inclu ...
- Codeforces A. Game on Tree(期望dfs)
题目描述: Game on Tree time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces Round #781(C. Tree Infection)
Codeforces Round #781 C. Tree Infection time limit per test 1 second memory limit per test 256 megab ...
- CodeForces 379F 树的直径 New Year Tree
题意:每次操作新加两个叶子节点,每次操作完以后询问树的直径. 维护树的直径的两个端点U,V,每次计算一下新加进来的叶子节点到U,V两点的距离,如果有更长的就更新. 因为根据树的直径的求法,若出现新的直 ...
- Codeforces 734E. Anton and Tree 搜索
E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ...
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
随机推荐
- dataframe 基本操作
package com.jason.example import org.apache.spark.sql.functions.broadcast class DFTest extends Spark ...
- VS一个奇怪的发布问题
同事的环境,发布项目时一直提示找不到某dll,在引用及bin里未发现黄色感叹号,后来发现问题是因为项目文件不小心包含了一个外部bin目录,并且该bin目录中的dll删除导致的.
- lombok的介绍、使用、简单分析和插件
学习下Lombok. 关于POJO Java面向对象编程中的特性中有封闭性和安全性.封闭性即对类中的域变量进行封闭操作,即用private来修饰他们.如此一来,其他类就不能对该变量访问了.这样,我们就 ...
- 基于FlexBox的无约束自适应
全手打原创,转载请标明出处:https://www.cnblogs.com/dreamsqin/p/11972664.html,多谢,=.=~ 一.背景描述 对于通用型带过滤条件的列表查询项目中, ...
- kvm虚拟机vnc配置
通过vnc方式访问虚拟主机上的KVM虚拟机 通过虚拟主机的IP地址与端口进行访问 1. 修改qemu.conf # vi /etc/libvirt/qemu.conf vnc_listen = &q ...
- JS中判断是中文数字的函数
function checkcnnum(str) { ; const zh = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九','十','百','千 ...
- PyTorch 之 DataLoader
DataLoader DataLoader 是 PyTorch 中读取数据的一个重要接口,该接口定义在 dataloader.py 文件中,该接口的目的: 将自定义的 Dataset 根据 batch ...
- Mysql中分组函数
查询每个工种的最高薪资
- (原创)使用C#开发高性能PLC上位机监控系统服务器应用程序
PLC服务器监控系统的特点: 1·使用微软C#面向对象开发语言开发应用程序.2·使用了健壮性与性能良好的SUPER SOCKET服务器通信框架,实现自定义应用层通信协议,支持多台PC客户端访问服务器, ...
- Java 多线程编程——多线程
如果要想在Java之中实现多线程的定义,那么就需要有一个专门的线程主体类进行线程的执行任务的定义,而这个主体类的定义是有要求的,必须实现特定的接口或者继承特定的父类才可以完成. 1. 继承Thread ...