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. 【BZOJ1081】[SCOI2005]超级格雷码(搜索)

    [BZOJ1081][SCOI2005]超级格雷码(搜索) 题面 BZOJ 洛谷 题解 找个规律吧,自己随便手玩一下,就按照正常的顺序枚举一下,发现分奇偶位考虑正序还是逆序就好了. #include& ...

  2. 【BZOJ1862】[ZJOI2006]游戏排名系统 (Splay)

    [BZOJ1862][ZJOI2006]游戏排名系统 (Splay) 题面 BZOJ 洛谷 题解 双倍经验题

  3. Vitrualbox 桥接网卡界面名称未指定、Filters currently installed on the system have reached the limit、不能为虚拟电脑 打开一个新任务

    1. 桥接网卡界面名称未指定 http://wenku.baidu.com/link?url=VFG0hknsDX3VPXQoX5f-g1wUX_LBl-lOj0ZqD222kM31iVCPJKVu3 ...

  4. 洛谷 P3629 [APIO2010]巡逻 解题报告

    P3629 [APIO2010]巡逻 题目描述 在一个地区中有 n 个村庄,编号为 1, 2, ..., n.有 n – 1 条道路连接着这些村 庄,每条道路刚好连接两个村庄,从任何一个村庄,都可以通 ...

  5. JAVA实现组合、排列、重复排列(多层循环)

    1.代码 package com.hdwang; import java.util.ArrayList; import java.util.Arrays; import java.util.List; ...

  6. bzoj2134: 单选错位(trie)

    预处理前后缀异或和,用trie得到前后缀最大答案,枚举中间点把左右两边加起来就是当前中间点的最大答案了...这个操作没见过,比较有意思,记录一下 #include<iostream> #i ...

  7. svn cleanup

    SVN 本地更新时,由于一些操作中断更新,如磁盘空间不够,用户取消. 可能会造成本地文件被锁定的情况.一般出现这种情况的解决方法: 1.可以使用SVN clean up来清除锁定. 2.如果不是本目录 ...

  8. ssm框架junit简单测试_我写

    第一步:导入相关jar包 主要是 junit包,和spring-test包 <dependecy> <groupId>junit</groupId> <art ...

  9. Debian8 系统修改语言设置成英文

    本文摘自 https://wiki.debian.org/ChangeLanguage ,感谢作者 * First, you have to set environment variables suc ...

  10. 内网服务器通过Squid代理访问外网

    环境说明 项目整体需部署Zabbix监控并配置微信报警,而Zabbix Server并不能访问外网,故运维小哥找了台能访问外网的服务器做Suqid代理,Zabbix Server服务器通过代理服务器访 ...