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. Oracle-表更名、转存数据

    --更名 ALTER TABLE T_LOGSRV_SERVICE RENAME TO T_LOGSRV_SERVICE_20170418_BAK; --创建同样的表 ;

  2. [jQuery]data-不能乱用啊

    使用jQuery easy ui做一个拖拽的功能 因为在要拖拽的元素中使用了date-options={OPTIONS}来存数据,因为没有替换{OPTIONS} jQuery貌似把我的}识别成方法的} ...

  3. 《Android源代码设计模式解析与实战》读书笔记(八)

    第八章.状态模式 1.定义 状态模式中的行为是由状态来决定,不同的状态下有不同的行为.当一个对象的内在状态改变时同意改变其行为,这个对象看起来像是改变了其类. 2.使用场景 1.一个对象的行为取决于它 ...

  4. 【待解决】maven创建web项目报错

    创建web项目时报错

  5. Java读源代码学设计模式:适配器Adapter

    适配器模式相关源代码:slf4j-1.6.1.hibernate-3.6.7 大家都知道.log4j是一个广泛使用的日志工具,除此之外.sun公司在JDK中也有自己的日志工具,也就是java.util ...

  6. iOS 获取当前时间格式化字符串

    iOS 获取当前时间格式化字符串 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保 ...

  7. xcode Could not launch "" ; has denied the launch request

    xcode Could not launch "" ;  “”“ has denied the launch request  (注意,这种方式不能调试) 1.编辑scheme 2 ...

  8. Linux - 设置SFTP服务用户目录权限

    我们有时会遇到这样的需求,限制一个Linux用户,让他只能在指定的目录下进行添加.修改.删除操作,并且只能使用sftp登录服务器,不能用ssh操作.这些可以通过配置sftp服务实现. 创建新用户www ...

  9. hdoj--1176--免费馅饼(动态规划)

    免费馅饼 Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status D ...

  10. JavaScript Simple

    ylbtech-JavaScript: 1.返回顶部 1.   2. 2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http: ...