New Year Tree

我们假设当前的直径两端为A, B, 那么现在加入v的两个儿子x, y。

求直径的话我们可以第一次dfs找到最远点这个点必定为直径上的点, 然而用这个点第二次dfs找到最远点, 这两个点就是直径。

因为A, B现在是直径的两端, 那么从v点dfs找到的最远点必定为A或者B, 那么从 x dfs找到的最远点也必定为A或者B, 那么如果有

新的直径其中一个端点不会变, 当前图和原图的差别就是x和y, 那么比较dist(A, x), dist(B, x)和未加入前直径的长度就能得到当前的直径。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long
using namespace std; const int N = 1e6 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; int f[N][], depth[N];
int q; int getLca(int u, int v) {
if(depth[u] < depth[v]) swap(u, v);
for(int i = ; i >= ; i--)
if(depth[f[u][i]] >= depth[v])
u = f[u][i];
if(u == v) return u;
for(int i = ; i >= ; i--)
if(f[u][i] != f[v][i])
u = f[u][i], v = f[v][i];
return f[u][];
} int dist(int u, int v) {
int lca = getLca(u, v);
return depth[u] + depth[v] - * depth[lca];
} int main() {
depth[] = ;
depth[] = depth[] = depth[] = ;
f[][] = f[][] = f[][] = ;
int A = , B = , dia = , n = ;
scanf("%d", &q);
while(q--) {
int v; scanf("%d", &v);
depth[n + ] = depth[v] + ;
depth[n + ] = depth[v] + ;
f[n + ][] = f[n + ][] = v;
for(int i = ; i < ; i++) {
f[n + ][i] = f[f[n + ][i - ]][i - ];
f[n + ][i] = f[f[n + ][i - ]][i - ];
}
int d1 = dist(A, n + );
int d2 = dist(B, n + );
if(d1 > dia) dia = d1, B = n + ;
else if(d2 > dia) dia = d2, A = n + ;
printf("%d\n", dia);
n += ;
}
return ;
} /*
*/

Codeforces 379F New Year Tree 树的直径的性质推理的更多相关文章

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

  2. LightOJ1094 - Farthest Nodes in a Tree(树的直径)

    http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no cycle ...

  3. Codeforces 379F New Year Tree

    F. New Year Tree time limit per test2 seconds memory limit per test256 megabytes You are a programme ...

  4. Codeforces 1413F - Roads and Ramen(树的直径+找性质)

    Codeforces 题目传送门 & 洛谷题目传送门 其实是一道还算一般的题罢--大概是最近刷长链剖分,被某道长链剖分与直径结合的题爆踩之后就点开了这题. 本题的难点就在于看出一个性质:最长路 ...

  5. 【bzoj2870】最长道路tree 树的直径+并查集

    题目描述 给定一棵N个点的树,求树上一条链使得链的长度乘链上所有点中的最小权值所得的积最大. 其中链长度定义为链上点的个数. 输入 第一行N 第二行N个数分别表示1~N的点权v[i] 接下来N-1行每 ...

  6. CodeForces-734E Anton and Tree 树的直径

    题目大意: 给定一棵有n个节点的树,有黑点白点两种节点. 每一次操作可以选择一个同种颜色的联通块将其染成同一种颜色 现在给定一个初始局面问最少多少步可以让树变为纯色. 题解: 首先我们拿到这棵树时先将 ...

  7. codeforces 14D(搜索+求树的直径模板)

    D. Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input o ...

  8. CodeForces 916E Jamie and Tree(树链剖分+LCA)

    To your surprise, Jamie is the final boss! Ehehehe. Jamie has given you a tree with n vertices, numb ...

  9. cf379F New Year Tree (树的直径+倍增lca)

    可以证明,如果合并两棵树,新的直径的端点一定是原来两树中直径的端点 可以把新加两个点的操作看成是把两个只有一个点的树合并到原来的树上,然后用其中的一个点去和原来树上的直径两端点更新直径就可以了 #in ...

随机推荐

  1. 【BZOJ1032】[JSOI2007]祖玛(动态规划)

    [BZOJ1032][JSOI2007]祖玛(动态规划) 题面 BZOJ 洛谷 题解 听说是道假题,假的原因是因为出题人可能没有考虑到祖玛的骚套路,比如可以先打几个球进去再一波消掉.也就是出题人基本默 ...

  2. 01-go语言开始-HelloWorld

    以输出HelloWorld为目标 Go的发展史 Go语言诞生(2007年的谷歌)的背景是由于软件开发的新挑战: 多核硬件架构 超大规模分布式计算集群 Web模式导致的前所未有的开发规模和更新速度 Go ...

  3. UiAutomator2.0入门

    总是听说UiAutomator这个框架,但从来没有使用过.找了篇入门,实践一下.实践之后感觉,uiautomator写测试代码,还是有点费劲.接口名比较多,比较长.网易的atx里使用的uiautoma ...

  4. springboot配置文件的配置

    转:https://www.cnblogs.com/zheting/p/6707036.html Spring Boot使用了一个全局的配置文件application.properties,放在src ...

  5. Java基础-SSM之Spring和Mybatis整合案例

    Java基础-SSM之Spring和Mybatis整合案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   在之前我分享过mybatis和Spring的配置案例,想必大家对它们的 ...

  6. python---补充locals()变量在变量分发中的使用

    在Django,tornado等框架中,变量分发渲染模板是一件再平常不过的事,但是当变量过多时,如何快速的进行变量传递 此时就可以用到locals()获取本地变量,将变量变为字典传入 def intr ...

  7. Hadoop源码阅读-HDFS-day2

    昨天看到了AbstractFileSystem,也知道应用访问文件是通过FileContext这个类,今天来看这个类的源代码,先看下这个类老长的注释说明 /** * The FileContext c ...

  8. Spark记录-Scala语法基础

    参考:http://docs.scala-lang.org/cheatsheets/index.html.http://docs.scala-lang.org/.http://www.scala-la ...

  9. cmd 概览---- 转

    打开"运行"对话框(Win+R),输入cmd,打开控制台命令窗口... 也可以通过cmd /c 命令 和 cmd /k 命令的方式来直接运行命令 注:/c表示执行完命令后关闭cmd ...

  10. 转载 你不知道的super

    http://funhacks.net/2016/11/09/super/ super仅被用于新式类 在类的继承中,如果重定义某个方法,该方法会覆盖父类的同名方法,但有时,我们希望能同时实现父类的功能 ...