Codeforces 379 F. New Year Tree
## [$>Codeforces \space 379 F. New Year Tree
题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ,一共有 \(q\) 次操作,每次选择一个节点,新建两个节点 \(n + 1, n + 2\) 向其连边,每次操作完之后求树的直径的长度
$1 \leq q \leq 5 \times 10^5 $
解题思路 :
观察发现,每次加点后,树的直径的端点至多只会变化一个
证明:首先显然新的直径的端点不可能是两个新加的点,也不可能是两个原本不是直径端点的点
所以如果要使得新的直径的端点变化两个的话,那么必然有一个端点是新加的点
假设有新加的点 \(u\) ,其父亲是 \(fa\) ,离 \(fa\) 最远的点一定是树的直径的一个端点,那么此时离 \(u\) 最远的点也一定是树的直径一个端点. 至此产生矛盾,所以每次加点后,树的直径的端点至多只会变化一个
所以只需要每次加点动态维护倍增数组,用 \(lca\) 暴力判是否能更新掉一个直径端点即可,复杂度是 \(O(nlogn)\)
/*program by mangoyang*/
#include<bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
int f = 0, ch = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if(f) x = -x;
}
#define N (1000005)
int f[N][23], dep[N], tot;
inline void addson(int u){
int v = ++tot;
dep[v] = dep[u] + 1, f[v][0] = u;
for(int j = 1; j <= 22; j++) f[v][j] = f[f[v][j-1]][j-1];
}
inline int Lca(int x, int y){
if(dep[x] < dep[y]) swap(x, y);
for(int i = 22; i >= 0; i--){
if(dep[f[x][i]] >= dep[y]) x = f[x][i];
if(dep[x] == dep[y]) break;
}
if(x == y) return x;
for(int i = 22; i >= 0; i--)
if(f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
return f[x][0];
}
inline int dis(int x, int y){
int lca = Lca(x, y); return dep[x] + dep[y] - 2 * dep[lca];
}
int main(){
dep[2] = dep[3] = dep[4] = 1;
f[2][0] = f[3][0] = f[4][0] = 1, tot = 4;
int n, l = 2, r = 4, ans = 2;
read(n);
for(int i = 1; i <= n; i++){
int x; read(x);
addson(x), addson(x);
int nl = tot - 1, nr = tot;
int s1 = dis(nl, r), s2 = dis(nl, l);
int s3 = dis(nr, l), s4 = dis(nr, r);
ans = Max(ans, Max(Max(s1, s2), Max(s3, s4)));
if(ans == s1) l = nl;
else if(ans == s2) r = nl;
else if(ans == s3) r = nr;
else if(ans == s4) l = nr;
printf("%d\n", ans);
}
return 0;
}
Codeforces 379 F. New Year Tree的更多相关文章
- codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(启发式合并)
codeforces 741D Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...
- codeforces 812E Sagheer and Apple Tree(思维、nim博弈)
codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x ...
- codeforces 220 C. Game on Tree
题目链接 codeforces 220 C. Game on Tree 题解 对于 1节点一定要选的 发现对于每个节点,被覆盖切选中其节点的概率为祖先个数分之一,也就是深度分之一 代码 #includ ...
- Codeforces 959 F. Mahmoud and Ehab and yet another xor task
\(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...
- Codeforces 835 F. Roads in the Kingdom
\(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...
- 2018 Multi-University Training Contest 3 Problem F. Grab The Tree 【YY+BFS】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6324 Problem F. Grab The Tree Time Limit: 2000/1000 MS ...
- Codeforces E. Alyona and a tree(二分树上差分)
题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces 731 F. Video Cards(前缀和)
Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...
- Codeforces 915 F. Imbalance Value of a Tree(并查集)
F. Imbalance Value of a Tree 题意: 给一颗带点权的树,求所有简单路径上最大点权和最小点权之差的总和. 思路: 所求问题可以看作求各路径上的最大值之和减各路径上的最小值之和 ...
随机推荐
- 【BZOJ2815】【ZJOI2012】灾难 [LCA]
灾难 Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 阿米巴是小强的好朋友. 阿米巴和小强 ...
- 第八周 yukun 20155335
- Less & Sass
CSS不是一种编程语言.它开发网页样式,但是没法用它编程.也就是说,CSS基本上是设计师的工具,它没有变量,也没有条件语句,只是一行行单纯的描述.有人就开始为CSS加入编程元素,这被叫做"C ...
- hdu 3371(prim算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Time Limit: 2000/1000 MS (Jav ...
- copy_from_user分析
前言 copy_from_user函数的目的是从用户空间拷贝数据到内核空间,失败返回没有被拷贝的字节数,成功返回0.它内部的实现当然不仅仅拷贝数据,还需要考虑到传入的用户空间地址是否有效,比如地址是不 ...
- 檢查 cpu 的全部 gpio 狀態及設定
$ adb root # cat /sys/kernel/debug/gpio
- binlog2sql 回滚误操作
参考过在资料: https://github.com/wuyongshenghub/mysqlbinlog2sql https://www.cnblogs.com/xuanzhi201111/p/66 ...
- mac os x 把reids nignx mongodb做成随机启动吧
~/Library/LaunchAgents 由用户自己定义的任务项 /Library/LaunchAgents 由管理员为用户定义的任务项 /Library/LaunchDaemons 由管理员定义 ...
- 生命周期(vue的钩子函数)
生命周期图示 创建前,创建后,挂载前,挂载后,更新前,更新后,销毁前,销毁后 beforeCreate:function(){ console.log('1-beforeCreate 组件还未被创建' ...
- 1.Python3标准库--前戏
Python有一个很大的优势便是在于其拥有丰富的第三方库,可以解决很多很多问题.其实Python的标准库也是非常丰富的,今后我将介绍一下Python的标准库. 这个教程使用的书籍就叫做<Pyth ...