题意:每次操作新加两个叶子节点,每次操作完以后询问树的直径。

维护树的直径的两个端点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的更多相关文章

  1. Sonya and Ice Cream CodeForces - 1004E 树的直径, 贪心

    题目链接 set维护最小值贪心, 刚开始用树的直径+单调队列没调出来... #include <iostream>#include <cstdio> #include < ...

  2. CodeForces 14D 树的直径 Two Paths

    给出一棵树,找出两条不相交即没有公共点的路径,使得两个路径的长度的乘积最大. 思路:枚举树中的边,将该边去掉,分成两棵树,分别求出这两棵树的直径,乘起来维护一个最大值即可. #include < ...

  3. Codeforces 633F 树的直径/树形DP

    题意:有两个小孩玩游戏,每个小孩可以选择一个起始点,并且下一个选择的点必须和自己选择的上一个点相邻,问两个选的点权和的最大值是多少? 思路:首先这个问题可以转化为求树上两不相交路径的点权和的最大值,对 ...

  4. 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 ...

  5. Codeforces 734E Anton and Tree(缩点+树的直径)

    题目链接: Anton and Tree 题意:给出一棵树由0和1构成,一次操作可以将树上一块相同的数字转换为另一个(0->1 , 1->0),求最少几次操作可以把这棵数转化为只有一个数字 ...

  6. Codeforces 804D Expected diameter of a tree(树的直径 + 二分 + map查询)

    题目链接 Expected diameter of a tree 题目意思就是给出一片森林, 若把任意两棵树合并(合并方法为在两个树上各自任选一点然后连一条新的边) 求这棵新的树的树的直径的期望长度. ...

  7. codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点

    J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...

  8. codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径

    题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...

  9. lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】

    1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...

随机推荐

  1. JVM类加载之创建对象的过程

    JVM对象的创建 new一个对象到底发生了什么? 1.VM遇到new关键字:首先检查是否能在常量池中定位到一个类的符号引用,并检查这个符号引用代表的类是否被加载,验证,准备,解析,初始化过,如果没有, ...

  2. window.open()弹出窗口参数说明及居中设置

    window.open()可以弹出一个新的窗口,并且通过参数控制窗口的各项属性. 最基本的弹出窗口代码 window.open('httP://codeo.cn/'); window.open()各参 ...

  3. 64位Windows系统下32位应用程序连接MySql

    1.首先得安装“Connector/ODBC”,就是Mysql的ODBC驱动,这个是与应用程序相关的,而不是与操作系统相关的,也就是说,不管你的系统是x64还是x86,只要你的应用程序是x86的那么, ...

  4. javaSe-字符型和布尔型

    其实java数据类型一节就可以全部写完了,为什么还需要字符型和布尔型呢,原因是这俩个都很重要: 字符型用char表示,字符分三种: 普通字符:char a = 'a',普通字符表示一个普通的字符,没有 ...

  5. 在Ubuntu12.04中搭建NFS的步骤

    1.首先安装nfs-kernel-server apt-get install nfs-kernel-server 2.然后创建一个目录: mkdir -p /opt/share 并赋予权限777: ...

  6. ABC3D创客项目:风力小车

    随着互联网.开源硬件.电子信息等技术成熟应用,以及创新教育的大力普及,创新正成为青少年生活中最热门的话题之一:尤其新兴的3D打印技术将创意者.生产者.消费者合三为一,成为创新教育的又一大助力,每个学生 ...

  7. 在.net平台上运行伪JAVA

    由于在一个项目局方要求使用JAVA平台, 而当前又都是.net平台的应用. 重新用JAVA开发工作量太大. 时间也来不及. 想到在.net中有url rewrite功能, 何不先"骗&quo ...

  8. python_94_类变量实例变量

    class Role: n=123#类变量 name='我是类name' list=[] def __init__(self,name,role,weapon,life_value=100,money ...

  9. Understanding NFS Caching

    Understanding NFS Caching Filesystem caching is a great tool for improving performance, but it is im ...

  10. CornerStone使用跳坑总结

    Cornerstone是专门为Mac用户设计的Subversion(SVN)的控制,使版本控制更加透明.cornerstone根Xcode相比,能够更好的忽略文件,所以除了项目经理第一次初始化项目的时 ...