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. oracle 每个类别取几条的语法怎么写

    select *from (select t.*,row_number() over(partition by t.公司名 order by 1) rn  from t)where rn<=10

  2. HGOI NOIP模拟4 题解

    NOIP国庆模拟赛Day5 题解 T1 马里奥 题目描述 马里奥将要参加 NOIP 了,他现在在一片大陆上,这个大陆上有着许多浮空岛,并且其中一座浮空岛上有一个传送门,马里奥想要到达传送门从而前往 N ...

  3. Libre 6007 「网络流 24 题」方格取数 / Luogu 2774 方格取数问题 (网络流,最大流)

    Libre 6007 「网络流 24 题」方格取数 / Luogu 2774 方格取数问题 (网络流,最大流) Description 在一个有 m*n 个方格的棋盘中,每个方格中有一个正整数.现要从 ...

  4. 项目实战SportsStore——订单处理模块

    前面的步骤如果顺利完成,你的网站运行之后应该能够正常显示下面三个页面: 1.产品列表 2.购物车内容页面 在某个商品后面点击“添加到购物车”则出现下图页面: 此页面上点击“继续购物”按钮则返回到产品列 ...

  5. 记踩坑--Flask Web开发:S6电子邮件 ----[Errno 11004] getaddrinfo failed

    必须要记录下踩过的坑,一来,为后来者铺路,二来,实在摔得疼,提醒自己写代码要谨小慎微. [Errno 11004] getaddrinfo failed 1.先排除邮箱账号和授权码的错误 测试如下代码 ...

  6. Oracle笔记 - unfinished

    1. plsql查看xmltype字段的xml格式时,出现中文乱码问题,可通过该字段.getClobVal():查询到的xml将是中文不乱码的. 2. extract函数查询xml某节点下的所有节点, ...

  7. python获取指定目录下所有文件名os.walk和os.listdir

    python获取指定目录下所有文件名os.walk和os.listdir 觉得有用的话,欢迎一起讨论相互学习~Follow Me os.walk 返回指定路径下所有文件和子文件夹中所有文件列表 其中文 ...

  8. unp的hello word

    最近无聊,再学学unp吧.学学网络编程.下面是一个demo: intro/daytimetcpcli.c #include "unp.h" int main(int argc, c ...

  9. 【转】[.Net] 确定当前网站的物理文件路径

    确定当前网站的物理文件路径 在应用程序中,您可能需要确定服务器上的文件或其他资源的路径.例如,如果应用程序以编程方式对文本文件进行读写操作,则必须为用于读取和写入的方法提供该文件的完整物理路径. 将物 ...

  10. Sql语句 表中相同的记录(某个字段)只显示一条,按照时间排序显示最大或最小

    原始表数据: