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. 用windows远程桌面连接ubuntu

    从Windows 7远程到Windows系统比较简单,只要对方电脑开启远程桌面功能就可以了,但Windows 7远程桌面连接到Ubuntu 14.04比较复杂一点,具体操作步骤如下. Ubuntu 1 ...

  2. 论文WAN Optimized Replication of Backup Datasets Using Stream-Informed Delta Compression

         这是EMC的备份小组发表在FAST12上的论文,主要是结合重删和差量数据压缩的方法,达到更高的数据压缩率.并且作者使用了一种基于数据流的差量数据压缩,消除了对索引的需求.通过测试达到的压缩效 ...

  3. spring mvc接收http参数

    1.http协议携带参数,无外乎两个三个存储地点:1.url上 ,2.header里 3.body里. 2.get请求是没有body的,数据全都放在url上,以?xx&xxx形式.注:get请 ...

  4. net_->ForwardBackward()的大致梳理

    net_->ForwardBackward()方法在net.hpp文件中 Dtype ForwardBackward() { Dtype loss; Forward(&loss); Ba ...

  5. Python标准库:内置函数range(stop) range(start, stop[, step])

    本函数是产生一系列序列的数组,返回迭代子.參数stop是终止的数字:參数start是指明開始数列開始值:參数step是数列之间的差值. 因此这个函数就是产生以start为起点.以stop为终点,以st ...

  6. 很实用的50个CSS代码片段

    原文:50 Useful CSS Snippets Every Designer Should Have          面对每年如此多的 新趋势 ,保持行业的率先是个非常困难问题. 站点设计者和前 ...

  7. SEO 爬虫原理介绍

    一篇关于网络爬虫程序的一些原理及体系结构纯技术文章,一些地方可能不会看的非常明确.对于SEO行业,常常和搜索引擎及其爬虫程序打交道,细致浏览下,一些不清楚而自己又非常想了解的地方,能够借助搜索来需找相 ...

  8. Github Pages 建立过程记录

    之前建立过一个測试页面. 如今在折腾CreateJS 试着把离线版的文档传到github pages上面. 第一步:创建Repository 第二步:本地初始化 主要命令:git init 第三步:复 ...

  9. jmeter函数和变量

    函数和变量广泛的应用在JMeter的传参过程,其中函数可以被认为是某种特殊的变量,它们可以被采样器或者其他测试元件所引用. 常用函数 1.__RamdomString() / __Ramdom() 获 ...

  10. Errors occurred during the build. Errors running builder &#39;Integrated External Tool Builder&#39; on proje

    Errors occurred during the build. Errors running builder 'Integrated External Tool Builder' on proje ...