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 ...
随机推荐
- 集合类源码(五)Collection之BlockingQueue(LinkedTransferQueue, PriorityBlockingQueue, SynchronousQueue)
LinkedTransferQueue 功能 全名 public class LinkedTransferQueue<E> extends AbstractQueue<E> i ...
- mysql获取日期语句汇总
汇总一些MySQL获取日期的SQL语句. -- 今天 SELECT DATE_FORMAT(NOW(),'%Y-%m-%d 00:00:00') AS '今天开始'; SELECT DATE_FORM ...
- mongodb 更新数据时int32变为double的解决办法
场景: 在命令手动的修改签到表的整型字段synState,multi参数是可以更新多条,如果是false则更新一条. db.getCollection("ClassRecordOneD ...
- asp.net webapi 随笔
第一次写博客,文笔有限,记录下学习的过程 话不多说,直接开干 首先用vs2017建立一个空网站项目,然后只勾选api 项目建立后,如下结构 其中WebApiConfig类配置了路由相关信息 publi ...
- NetCoreApi框架搭建(二、Nlog使用配置)
本文只配置了简单文件存储 1.添加nuget包 2.添加日志配置文件nlog.config 这里配置了三个target区分不同的日志,具体配置需要自己研究,推荐链接https://www.cnblog ...
- python 排序 插入排序与希尔排序
希尔排序是插入排序的升级版,先来了解插入排序 插入排序 算法思想: 插入排序再面对几乎已经有序的数据效率非常高,可以达到线性排序的效率 将数组元素插入已经有序的部分中,具体的过程是在有序的部分中通过比 ...
- 纯C语言实现线性表
#include <stdio.h> #include <stdlib.h> #define MAXSIZE 100 typedef int ElemType; typedef ...
- 2019 中细软java面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.中细软等公司offer,岗位是Java后端开发,因为发展原因最终选择去了中细软,入职一年时间了,也成为了面试官 ...
- 自动居中标题和内容;aspxgridview允许定义两个关键字为主键的格式
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- bugku insertsql
题目链接 0X00题目给出的PHP代码 error_reporting(0); function getIp(){ $ip = ''; if(isset($_SERVER['HTTP_X_FORWAR ...