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

维护树的直径的两个端点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. 大都市 meg

    Description 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景. 昔日,乡下有依次编号为1.. ...

  2. SLF4J user manual 专题

    System Out and Err Redirected to SLF4J The sysout-over-slf4j module allows a user to redirect all ca ...

  3. c# Redis操作类

    需要添加StackExchange.Redis.dll引用 using System; using System.Collections.Generic; using System.IO; using ...

  4. Java面向对象(继承、抽象类)

    面向对象 今日内容介绍 u 继承 u 抽象类 第1章 继承 1.1 继承的概念 在现实生活中,继承一般指的是子女继承父辈的财产.在程序中,继承描述的是事物之间的所属关系,通过继承可以使多种事物之间形成 ...

  5. java构造方法之我见

    java中构造方法是作为除了成员方法之外的一种特殊方法,方法名与类名相同.一般类中如果没有明确定义构造方法时,编译器默认为无参构造方法.当我们调用new方法创建对象就是通过构造方法完成的.因此,当有对 ...

  6. CF1166C A Tale of Two Lands

    思路: 搞了半天发现和绝对值无关. http://codeforces.com/blog/entry/67081 实现: #include <bits/stdc++.h> using na ...

  7. Mind must be master of the body, strong mind can separate the body from its suffering.

    Mind must be master of the body, strong mind can separate the body from its suffering.意志是身体的主人,有顽强的意 ...

  8. Centos 7 搭建git服务器及使用gitolite控制权限

    一.安装git yum install git git --version #查看git版本 二.升级git(可选,如果之前已经安装git,需要升级git到最新版本) git clone https: ...

  9. 【Python图像特征的音乐序列生成】数据集制作的一些tricks

    关于数据集的制作,我决定去掉很多不必要的东西,比如和弦,于是我选择了melody部分的旋律. 有了midi文件,我现在要abc序列,所以我要通过midi2abc转换一下文件. 批处理程序效果如下: 文 ...

  10. [转]maven项目部署到tomcat

    其实maven项目部署到tomcat的方式很多,我从一开始的打war包到tomcat/webapps目录,到使用tomcat-maven插件,到直接使用servers部署,一路来走过很多弯路. 下面就 ...