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 题意: 给一颗带点权的树,求所有简单路径上最大点权和最小点权之差的总和. 思路: 所求问题可以看作求各路径上的最大值之和减各路径上的最小值之和 ...
随机推荐
- Html5学习3(拖放、Video(视频)、Input类型(color、datetime、email、month 、number 、range 、search、Tel、time、url、week ))
1.Html拖放 <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> < ...
- JS之window对象
window对象 window属性: opener:打开当前窗口的源窗口,如果这个窗口是由别的网页点击链接跳转过来的,或者是从另外一个页面点击打开窗口打开的,opener就是找到源页面的.如果当前窗口 ...
- js中三种定义变量 const, var, let 的区别
js中三种定义变量的方式const, var, let的区别 1.const定义的变量不可以修改,而且必须初始化. 1 const b = 2;//正确 2 // const b;//错误,必须初始化 ...
- 【JDK】JDK7与JDK8环境共存与切换:先安装jdk7,配置好环境变量后再安装jdk8
1.先安装JDK7 下载jdk-7u79-windows-i586.exe,安装后配置好环境变量JAVA_HOME,CLASSPATH,PATH java -version javac 指令都正常 2 ...
- 【转】gif文件格式详解
1.概述 ~~~~~~~~ GIF(Graphics Interchange Format,图形交换格式)文件是由 CompuServe公司开发的图形文件格式,版权所有,任何商业目的使用均须 Comp ...
- perl中设置POST登录时的重定向
默认地, perl提交post登录时是不会重定向的 要让它重定向, 可以用如下方法: my $cookie = HTTP::Cookies->new(); push @{$ua->requ ...
- 虚拟机出现intel vt -x 处于禁用状态打不开处理方式
处理方式 . 1 进入bios 以华硕主板为例 进入高级模式找到cpu虚拟技术 打开虚拟技术支持 其它电脑找到这个
- 网络知识===wireshark抓包数据分析(一)
wireshark分析: 上图是我进行一个HTTP协议的下载,文件内容大概是1.7M左右. 抓包数据: https://files.cnblogs.com/files/botoo/wireshark% ...
- Redis 3.0 编译安装
Redis 3.0 编译安装 http://www.xuchanggang.cn/archives/991.html
- 图论-最小生成树-Kruskal算法
有关概念: 最小生成树:在连通图G中,连接图G所有顶点且总权最小的边构成的树 思路: 首先对边按权从小到大排序,紧接着枚举每一条边,如果两个结点的祖先结点不同(并查集),则连上此边,直到边数等于结点数 ...