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 ...
随机推荐
- ScheduledThreadPoolExecutor源码
添加元素,先添加到数组末尾,然后上调整堆. 取对首元素,把最后一个元素放到0位置,然后下调整堆. 移除中间元素,把最后一个元素放到中间位置,然后下调整堆,下调整堆没动(已经是最大的),就在上调整堆.下 ...
- centos7上配置mysql8的主从复制
注意:1.主库:10.1.131.75,从库:10.1.131.762.server-id必须是纯数字,并且主从两个server-id在局域网内要唯一. [主节点]vi /etc/my.cnf[mys ...
- Django的学习——全局的static和templates的使用
一.问题 首先我们在进行Django框架搭建的时候我们需要建立一个全局的变量,一是为了实现代码的复用,二是为了方便管理,如下图的样式 二.解决 1.修改setting里面的配置文件①templates ...
- Oracle学习笔记(六)
Oralce行定位与rowid: drop table t purge; create table t as select * from dba_objects; create index idx_o ...
- spring-security.xml——安全性框架配置文件
<?xml version="1.0" encoding="UTF-8"?><beans:beans xmlns="http://w ...
- 洛谷 P1002过河卒
洛谷 P1002过河卒 题目描述 棋盘上AA点有一个过河卒,需要走到目标BB点.卒行走的规则:可以向下.或者向右.同时在棋盘上CC点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点 ...
- 架构师小跟班:送你一份2019年阿里巴巴最新Java面试题,以供参考
大家都说大厂面试造飞机,工作拧螺丝.这话只对了一部分,大厂是平时拧螺丝,一旦需要飞机时也能造的起来. 目录 技术一面(23问) 技术二面(3大块) JAVA开发技术面试中可能问到的问题(17问) JA ...
- 斐波那契查找(Fibonacci Search)
斐波那契查找 斐波那契查找就是在二分查找的基础上根据斐波那契数列进行分割的. 在斐波那契数列找一个等于略大于查找表中元素个数的数F[n],将原查找表扩展为长度为F[n](如果要补充元素,则补充重复 ...
- vue+element省市县的二级联动功能
项目中有选择省市县的需求,先选择省,再选择县 解决这个需求也不是很难,总体思路就是看后端接口, 一般后端接口都是请求参数为 0 返回省的数据,不为 0 的话返回相对应的市的数据 template代码: ...
- 配置WDA程序到NWBC
NWBC是通过角色来做的权限管理,包括菜单项. 如果用户没有分配对应的,登陆后看到的东西很少,或者空白. 这里需要先将BC的个性化参数全部配齐,方便使用.BC最左上角,点击,设置,个性化设置. 如下几 ...