Codeforces 379F 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 树的直径的性质推理的更多相关文章
- 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 ...
- LightOJ1094 - Farthest Nodes in a Tree(树的直径)
http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no cycle ...
- Codeforces 379F New Year Tree
F. New Year Tree time limit per test2 seconds memory limit per test256 megabytes You are a programme ...
- Codeforces 1413F - Roads and Ramen(树的直径+找性质)
Codeforces 题目传送门 & 洛谷题目传送门 其实是一道还算一般的题罢--大概是最近刷长链剖分,被某道长链剖分与直径结合的题爆踩之后就点开了这题. 本题的难点就在于看出一个性质:最长路 ...
- 【bzoj2870】最长道路tree 树的直径+并查集
题目描述 给定一棵N个点的树,求树上一条链使得链的长度乘链上所有点中的最小权值所得的积最大. 其中链长度定义为链上点的个数. 输入 第一行N 第二行N个数分别表示1~N的点权v[i] 接下来N-1行每 ...
- CodeForces-734E Anton and Tree 树的直径
题目大意: 给定一棵有n个节点的树,有黑点白点两种节点. 每一次操作可以选择一个同种颜色的联通块将其染成同一种颜色 现在给定一个初始局面问最少多少步可以让树变为纯色. 题解: 首先我们拿到这棵树时先将 ...
- codeforces 14D(搜索+求树的直径模板)
D. Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input o ...
- 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 ...
- cf379F New Year Tree (树的直径+倍增lca)
可以证明,如果合并两棵树,新的直径的端点一定是原来两树中直径的端点 可以把新加两个点的操作看成是把两个只有一个点的树合并到原来的树上,然后用其中的一个点去和原来树上的直径两端点更新直径就可以了 #in ...
随机推荐
- Mac OS利用ssh访问ubuntu虚拟机及云端操作
1.桥接模式 将该虚拟机的网口设置成桥接模式(Bridged Adapter),以确保主机可以ping通虚拟机: 2.安装ssh 在ubuntu虚拟机上安装ssh server: sudo apt-g ...
- Java基础-Java中23种设计模式之常用的设计模式
Java基础-Java中23种设计模式之常用的设计模式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.设计模式分类 设计模式是针对特定场景给出的专家级的解决方案.总的来说设 ...
- Kafka 0.8 sever:源代码High level分析
本文主要介绍了Kafka High level的代码架构和主要的类. 这张图是0.8版本的架构 Boker 架构 1 network layer Kafka使用NIO自己实现了网络层的代码, 而不是采 ...
- 在ajax请求后台时在请求标头RequestHeader加token
情景:为了保证系统数据的安全性,一般前后台之间的数据访问会有授权与验证,这里的Token机制相对于Cookie支持跨域访问,在RESTful API里面,验证一般可以使用POST请求来通过验证,使服务 ...
- bzoj千题计划189:bzoj1867: [Noi1999]钉子和小球
http://www.lydsy.com/JudgeOnline/problem.php?id=1867 dp[i][j] 落到(i,j)的方案数 dp[i][j]=0.5*dp[i-1][j] ...
- c#的as,is 运算符
- [转载]AngularJS 开发者最常犯的 10 个错误
http://www.oschina.net/translate/top-10-mistakes-angularjs-developers-make
- TED_Topic10:The case for engineering our food
By Pamela Ronald Pamela Ronald studies the genes that make plants more resistant to disease and stre ...
- Linux - 用户操作
常用命令 users # 显示所有的登录用户 groups # 列出当前用户和他所属的组 who -q # 显示所有的登录用户 groupadd # 添加组 useradd user # 建立用户 p ...
- 洛谷 P1525 【关押罪犯】种类并查集
题解 P1525 [关押罪犯]:种类并查集 前言: 在数据结构并查集中,种类并查集属于扩展域并查集一类. 比较典型的题目就是:食物链(比本题难一些,有三个种类存在) 首先讲一下本题的贪心,这个是必须要 ...