CodeForces 379F 树的直径 New Year Tree
题意:每次操作新加两个叶子节点,每次操作完以后询问树的直径。
维护树的直径的两个端点U,V,每次计算一下新加进来的叶子节点到U,V两点的距离,如果有更长的就更新。
因为根据树的直径的求法,若出现新的直径,一定是到U或者到V距离最远。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> const int maxn = + ;
const int logmaxn = ; int n, Q; int L[maxn];
int fa[maxn];
int anc[maxn][logmaxn]; void add(int u, int pa)
{
fa[u] = pa;
L[u] = L[pa] + ;
anc[u][] = pa;
for(int j = ; ( << j) < n; j++) if(anc[u][j-])
anc[u][j] = anc[anc[u][j-]][j-];
} int LCA(int p, int q)
{
if(L[p] < L[q]) std::swap(p, q);
int log;
for(log = ; ( << log) <= L[p]; log++); log--;
for(int i = log; i >= ; i--)
if(L[p] - ( << i) >= L[q]) p = anc[p][i];
if(p == q) return p;
for(int i = log; i >= ; i--)
if(anc[p][i] && anc[p][i] != anc[q][i])
p = anc[p][i], q = anc[q][i];
return fa[p];
} int distance(int u, int v)
{
int l = LCA(u, v);
return L[u] + L[v] - L[l] * ;
} int main()
{
scanf("%d", &Q);
n = ;
fa[] = fa[] = fa[] = ;
L[] = L[] = L[] = ;
anc[][] = anc[][] = anc[][] = ; int U = , V = , diameter = ;
while(Q--)
{
int p; scanf("%d", &p);
add(++n, p);
add(++n, p);
int l1 = distance(n, U), l2 = distance(n, V);
if(l1 >= l2 && l1 >= diameter)
{
V = n;
diameter = l1;
}
else if(l2 >= l1 && l2 >= diameter)
{
U = n;
diameter = l2;
}
printf("%d\n", diameter);
} return ;
}
代码君
CodeForces 379F 树的直径 New Year Tree的更多相关文章
- Sonya and Ice Cream CodeForces - 1004E 树的直径, 贪心
题目链接 set维护最小值贪心, 刚开始用树的直径+单调队列没调出来... #include <iostream>#include <cstdio> #include < ...
- CodeForces 14D 树的直径 Two Paths
给出一棵树,找出两条不相交即没有公共点的路径,使得两个路径的长度的乘积最大. 思路:枚举树中的边,将该边去掉,分成两棵树,分别求出这两棵树的直径,乘起来维护一个最大值即可. #include < ...
- Codeforces 633F 树的直径/树形DP
题意:有两个小孩玩游戏,每个小孩可以选择一个起始点,并且下一个选择的点必须和自己选择的上一个点相邻,问两个选的点权和的最大值是多少? 思路:首先这个问题可以转化为求树上两不相交路径的点权和的最大值,对 ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree 树的直径
E. Anton and Tree time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces 734E Anton and Tree(缩点+树的直径)
题目链接: Anton and Tree 题意:给出一棵树由0和1构成,一次操作可以将树上一块相同的数字转换为另一个(0->1 , 1->0),求最少几次操作可以把这棵数转化为只有一个数字 ...
- Codeforces 804D Expected diameter of a tree(树的直径 + 二分 + map查询)
题目链接 Expected diameter of a tree 题目意思就是给出一片森林, 若把任意两棵树合并(合并方法为在两个树上各自任选一点然后连一条新的边) 求这棵新的树的树的直径的期望长度. ...
- codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点
J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...
- codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径
题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...
- lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】
1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...
随机推荐
- 前后端分离 vue+springboot 跨域 session+cookie失效问题
环境: 前端 vue ip地址:192.168.1.205 后端 springboot2.0 ip地址:192.168.1.217 主要开发后端. 问题: 首先登陆成功时将用户存在session ...
- 【踩坑】springMVC 接收String参数没有判断为空
今天在调试iReview项目的接口时,发现新增词条和新增库的时候,某些字段即使留空POST到后台时也能当做不为空. 经过排查,发现后台是使用 String 变量名 == null 这样的语句去判断变量 ...
- 网页转PDF作为邮件附件
Nuget 引入 OpenHtmlToPdf using (WebClient wc = new WebClient()) { wc.Encoding = Encoding.UTF8; wc.UseD ...
- ABAP数据类型
数据类型表: 类型缩写 类型 默认长度 允许长度 初始值 描述 C 文本型 1 Space 字符串数据,如'Program' D 日期型 8 8 '00000000' 日期数据,格式为YYYYMM ...
- JavaWeb_02_CSS学习
CSS简介 Cascading Style Sheets (CSS)层叠样式表:en.wikipedia.org 层叠:一层一层的叠加样式 样式表:提供更多属性和属性值实现更多样式变化 css将显示样 ...
- ios 11 12以后下拉刷新不回位的解决方法
原因: iOS11弃用了automaticallyAdjustsScrollViewInsets属性,新增contentInsetAdjustmentBehavior来替代它 //解决方案 添加如下 ...
- 【开发小结】Two Steps from Deadline
进度条可以救我也可以杀死我 # START 2018年4月17日晚我测试了11组四则运算的UI,每个exe程序生成的每一道题都有恐怖的倒计时.PSP表格清晰的记录了开发过程中消耗的时间,但是在结对作业 ...
- 宿主机Windows访问虚拟机Linux文件(一)
如果用户使用windows操作系统,但是在虚拟机下配置Linux内核操作操作系统,往往需要实现通过宿主机Windows操作系统访问Linux内核操作系统中资源.本次实验实现的是宿主机windows 1 ...
- 给广大码农分享福利:一个业界良心的github仓库,中文计算机资料
我今天查资料时无意发现的,https://github.com/CyC2018/CS-Notes 这个仓库包含了下列几个维度的计算机学习资料: 深受国内程序员喜爱,已经有超过3万多star了. 1. ...
- CodeForces 66C Petya and File System (实现)
模拟题,map搞一搞.要想清楚一个结点应该是要通过一个字符串找到下一个结点,题目保证所以文件夹非空,所以只要判断一个结点是不是叶子结点就可以判断它是不是文件,用了点c11的特性. #include&l ...