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 简单倍增 + 一些有趣的推导的更多相关文章

  1. codeforces 690C3 C3. Brain Network (hard)(lca)

    题目链接: C3. Brain Network (hard) time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  2. codeforces 690C2 C2. Brain Network (medium)(bfs+树的直径)

    题目链接: C2. Brain Network (medium) time limit per test 2 seconds memory limit per test 256 megabytes i ...

  3. codeforces 690C1 C1. Brain Network (easy)(水题)

    题目链接: C1. Brain Network (easy) time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  4. Codeforces 690 C3. Brain Network (hard) LCA

    C3. Brain Network (hard)   Breaking news from zombie neurology! It turns out that – contrary to prev ...

  5. Brain Network (medium)(DFS)

    H - Brain Network (medium) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d &am ...

  6. Brain Network (easy)(并查集水题)

    G - Brain Network (easy) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  7. Brain Network (medium)

    Brain Network (medium) Further research on zombie thought processes yielded interesting results. As ...

  8. Brain Network (easy)

    Brain Network (easy) One particularly well-known fact about zombies is that they move and think terr ...

  9. CF 690C3. Brain Network (hard) from Helvetic Coding Contest 2016 online mirror (teams, unrated)

    题目描述 Brain Network (hard) 这个问题就是给出一个不断加边的树,保证每一次加边之后都只有一个连通块(每一次连的点都是之前出现过的),问每一次加边之后树的直径. 算法 每一次增加一 ...

随机推荐

  1. Java设计模式之 — 策略(Strategy)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8986285 今天你的leader兴致冲冲地找到你,希望你可以帮他一个小忙,他现在急 ...

  2. hdu 1568关于斐波那契数列的公式及其思维技巧

    先看对数的性质,loga(b^c)=c*loga(b),loga(b*c)=loga(b)+loga(c); 假设给出一个数10234432,那么log10(10234432)=log10(1.023 ...

  3. Bi-shoe and Phi-shoe 欧拉函数 素数

    Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popular co ...

  4. js 跨浏览器获取事件信息模块

    var EventUtil = { addHandler: function(element, type, handler) { if (element.addEventListener) { ele ...

  5. IndexError:string index out of range

    IndexError:string index out of range 出现在下标越界的情况,如 item[1],可能为空的时候下标就会越界

  6. Android Studio 编译Gradle提示编码错误

    在Android Studio中,假设我们的代码有中文凝视.显示的时候全然正常,而编译的时候Gradle就会报错,可是又能够编译成功,并且Gradle报的错误信息,也是乱码! 这样我们就不好寻找错误原 ...

  7. spring之注解(一)概述

    Spring的核心是依赖注入(DI),而依赖注入的基础是依赖信息的配置.这些配置称之为元数据. 在之前的的学习中.一直採用的是基于xml的配置,这些元数据配置在spring内部被注冊成为BeanDef ...

  8. NS3网络仿真(3): NetAnim

    快乐虾 http://blog.csdn.net/lights_joy/ 欢迎转载,但请保留作者信息 在NS3提供的演示样例first.py中,并没有生成NetAnim所须要的xml文件,本节我们尝试 ...

  9. 第一次Java作业——简单的登录界面

    千里之行,始于足下,从小做起,一点一滴学编程. import javax.swing.*; import java.awt.*; public class Homework{ public stati ...

  10. c++迭代器失效问题

    参考两篇文章:https://blog.csdn.net/skyroben/article/details/70877008 https://lichanghao.github.io/2016/08/ ...