Brain Network (hard) CodeForces - 690C 简单倍增 + 一些有趣的推导
Code:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 200000 + 4;
const int logn = 19;
int F[30][maxn], dep[maxn];
inline int lca(int a,int b)
{
if(dep[a] > dep[b]) swap(a,b);
if(dep[a] != dep[b])
{
for(int i = logn;i >= 0;--i) if(dep[F[i][b]] >= dep[a]) b = F[i][b];
}
if(a == b) return a;
for(int i = logn; i >= 0; --i)
{
if(F[i][a] != F[i][b]) a = F[i][a], b = F[i][b];
}
return F[0][a];
}
int main()
{
// freopen("input.txt","r",stdin);
int n, point1 = 1, point2 = 1;
scanf("%d",&n);
dep[1] = 1;
for(int i = 2;i <= n; ++i)
{
int fa; scanf("%d",&fa);
F[0][i] = fa, dep[i] = dep[fa] + 1;
for(int j = 1;j <= logn; ++j)F[j][i] = F[j - 1][F[j-1][i]];
int ans1 = dep[point1] + dep[point2] - 2 * dep[lca(point1,point2)];
int ans2 = dep[point1] + dep[i] - 2 * dep[lca(point1, i)];
int ans3 = dep[point2] + dep[i] - 2 * dep[lca(point2, i)];
int maxv = max(ans1, max(ans2, ans3));
printf("%d\n",maxv);
if(ans1 == maxv) continue;
if(ans2 == maxv) point2 = i;
else if(ans3 == maxv) point1 = i;
}
return 0;
}
Brain Network (hard) CodeForces - 690C 简单倍增 + 一些有趣的推导的更多相关文章
- codeforces 690C3 C3. Brain Network (hard)(lca)
题目链接: C3. Brain Network (hard) time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- codeforces 690C2 C2. Brain Network (medium)(bfs+树的直径)
题目链接: C2. Brain Network (medium) time limit per test 2 seconds memory limit per test 256 megabytes i ...
- codeforces 690C1 C1. Brain Network (easy)(水题)
题目链接: C1. Brain Network (easy) time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- Codeforces 690 C3. Brain Network (hard) LCA
C3. Brain Network (hard) Breaking news from zombie neurology! It turns out that – contrary to prev ...
- Brain Network (medium)(DFS)
H - Brain Network (medium) Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d &am ...
- Brain Network (easy)(并查集水题)
G - Brain Network (easy) Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- Brain Network (medium)
Brain Network (medium) Further research on zombie thought processes yielded interesting results. As ...
- Brain Network (easy)
Brain Network (easy) One particularly well-known fact about zombies is that they move and think terr ...
- CF 690C3. Brain Network (hard) from Helvetic Coding Contest 2016 online mirror (teams, unrated)
题目描述 Brain Network (hard) 这个问题就是给出一个不断加边的树,保证每一次加边之后都只有一个连通块(每一次连的点都是之前出现过的),问每一次加边之后树的直径. 算法 每一次增加一 ...
随机推荐
- vue 注册全局组件
注册全局组件有啥好处呢? 提高代码的复用性:哪里需要写哪里,贼方便,就写一个标签:减少代码量:可以再配合slot一起使用,咦~~,舒服 为了让整个项目的可读性,我创建一个文件统一存放全局组件 1.创建 ...
- 【codeforces 793A】Oleg and shares
[题目链接]:http://codeforces.com/contest/793/problem/A [题意] 每次你可以对1..n中的任意一个数字进行减少k操作; 问你最后可不可能所有的数字都变成一 ...
- APIO2017听课笔记关键词
- [bzoj1606][Usaco2008 Dec]Hay For Sale 购买干草_动态规划_背包dp
Hay For Sale 购买干草 bzoj-1606 Usaco-2008 Dec 题目大意:约翰遭受了重大的损失:蟑螂吃掉了他所有的干草,留下一群饥饿的牛.他乘着容量为C(1≤C≤50000)个单 ...
- 洛谷 P2412 查单词
P2412 查单词 题目背景 滚粗了的HansBug在收拾旧英语书,然而他发现了什么奇妙的东西. 题目描述 udp2.T3如果遇到相同的字符串,输出后面的 蒟蒻HansBug在一本英语书里面找到了一个 ...
- Foundation框架和文件操作
NSString --实例化方法-------------- NSString *str = [[NSString alloc] init]; NSString *str = [[[NSString ...
- SPOJ COWPIC(逆序对变形题)
SPOJ COWPIC 题目链接 题意:一个序列,相邻能够交换.问最少交换几次使得变成循环的1-n的当中一种 思路:对于原来正常的变换成1-n而言,答案就是逆序对了,而多了这么一个变形,事实上仅仅须要 ...
- 齐头并进完成任务——Java多线程(一)
多线程(Multithread)指的是在单个进程中同时运行多个不同的线程,执行不同的任务.多线程意味着一个程序的多行语句块并发执行. 一.实现多线程 1.通过继承Thread类实现多线程. Threa ...
- POJ3185 The Water Bowls 反转(开关)
Description The cows have a line of 20 water bowls from which they drink. The bowls can be either ri ...
- Highcharts构建空饼图
Highcharts构建空饼图 空饼图就是不包括不论什么节点的饼图. 在Highcharts中,假设数据列不包括数据,会自己主动显示空白. 这样浏览者无法推断当前图表为什么类型.绘制一个空饼图的变通方 ...