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 题意: 给一颗带点权的树,求所有简单路径上最大点权和最小点权之差的总和. 思路: 所求问题可以看作求各路径上的最大值之和减各路径上的最小值之和 ...
随机推荐
- 【BZOJ4819】【SDOI2017】新生舞会 [费用流][分数规划]
新生舞会 Time Limit: 10 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 学校组织了一次新生舞会,Cathy ...
- Problem B. Harvest of Apples(杭电2018年多校+组合数+逆元+莫队)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333 题目: 题意:求C(n,0)+C(n,1)+……+C(n,m)的值. 思路:由于t和n数值范围太 ...
- 新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛) F.猴子排序的期望
题目链接:https://www.nowcoder.com/acm/contest/116/F 题目描述 我们知道有一种神奇的排序方法叫做猴子排序,就是把待排序的数字写在卡片上,然后让猴子把卡片扔在空 ...
- Plant (矩阵快速幂)
题目链接:http://codeforces.com/problemset/problem/185/A 题目: Dwarfs have planted a very interesting plant ...
- js中三种定义变量 const, var, let 的区别
js中三种定义变量的方式const, var, let的区别 1.const定义的变量不可以修改,而且必须初始化. 1 const b = 2;//正确 2 // const b;//错误,必须初始化 ...
- 排序中topK那点事(转)
问题描述:有 N (N>1000000)个数,求出其中的前K个最小的数(又被称作topK问题). 这类问题似乎是备受面试官的青睐,相信面试过互联网公司的同学都会遇到这来问题.下面由浅入深,分析一 ...
- python进行机器学习(一)之数据预处理
一.加载数据 houseprice=pd.read_csv('../input/train.csv') #加载后放入dataframe里 all_data=pd.read_csv('a.csv', h ...
- [干货,阅后进BAT不是梦]面试心得与总结---BAT、网易、蘑菇街
本文转载自:公众号:JANiubility 前言 之前实习的时候就想着写一篇面经,后来忙就给忘了,现在找完工作了,也是该静下心总结一下走过的路程了,我全盘托出,奉上这篇诚意之作,希望能给未来找工作的人 ...
- Tomcat8配置默认项目
<!-- 配置默认访问项目 --> <Host name="localhost" appBase="webapps" unpackWARs=& ...
- sublime Text3 === 无法输入input的问题解决办法
sublimetext无法对input或者raw_input执行.因此搜了很多方法后,解决了这个问题: 1.先下载插件sublimerepl ,如果无法下载,请点击https://github.com ...