C3. Brain Network (hard)
 

Breaking news from zombie neurology! It turns out that – contrary to previous beliefs – every zombie is born with a single brain, and only later it evolves into a complicated brain structure. In fact, whenever a zombie consumes a brain, a new brain appears in its nervous system and gets immediately connected to one of the already existing brains using a single brain connector. Researchers are now interested in monitoring the brain latency of a zombie. Your task is to write a program which, given a history of evolution of a zombie's nervous system, computes its brain latency at every stage.

Input

The first line of the input contains one number n – the number of brains in the final nervous system (2 ≤ n ≤ 200000). In the second line a history of zombie's nervous system evolution is given. For convenience, we number all the brains by 1, 2, ..., n in the same order as they appear in the nervous system (the zombie is born with a single brain, number 1, and subsequently brains 2, 3, ..., n are added). The second line contains n - 1 space-separated numbers p2, p3, ..., pn, meaning that after a new brain k is added to the system, it gets connected to a parent-brain .

Output

Output n - 1 space-separated numbers – the brain latencies after the brain number k is added, for k = 2, 3, ..., n.

Example
input
6
1
2
2
1
5
output
1 2 2 3 4 

题意:

  给你一个根节点1,之后每次加一条边,结点的父结点是树中已经得到的结点,问你加完边之后每一次的直径

题解:

  新直径与加边之前的直径的关系,假设未加边之前是由X,Y这两点组成的链最长,那么答案必然是 max(dis(i,X),dis(i,Y),dis(X,Y));  

  这个画图作作假设就看得出

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
const int N=4e5+,mods=,inf=2e9+; int fa[N][],dep[N],n,f[N];
vector<int > G[N];
void Lca_dfs(int u,int p,int d) {
fa[u][] = p, dep[u] = d;
for(int i = ; i < G[u].size(); ++i) {
int to = G[u][i];
if(to == p) continue;
Lca_dfs(to,u,d+);
}
}
void Lca_init() {
Lca_dfs(,,);
for(int i = ; i <= ; ++i) {
for(int j = ; j <= n; ++j) {
if(fa[j][i-]) {
fa[j][i] = fa[fa[j][i-]][i-];
} else {
fa[j][i] = ;
}
}
}
}
int Lca(int x,int y) {
if(dep[x] > dep[y]) swap(x,y);
for(int k = ; k < ; ++k) {
if((dep[y] - dep[x])>>k&)
y = fa[y][k];
}
if(x == y) return x;
for(int k = ; k >= ; --k) {
if(fa[x][k] != fa[y][k]) {
x = fa[x][k];
y = fa[y][k];
}
}
return fa[x][];
}
int main() {
scanf("%d",&n);
for(int i = ; i <= n; ++i){
scanf("%d",&f[i]);
G[f[i]].push_back(i);
}
Lca_init();
int ans = ,x = ,y = ;
for(int i = ; i <= n; ++i) {
int ux = Lca(i,x);
int uy = Lca(i,y);
int nowx,nowy;
if(x == ux) {
if(ans < dep[i] - dep[x]) {
ans = dep[i] - dep[x];
nowx = i;nowy = x;
}
}else {
if(dep[i] + dep[x] - *dep[ux] > ans) {
ans = dep[i] + dep[x] - *dep[ux];
nowx = i;
nowy = x;
}
}
if(y == uy) {
if(ans < dep[i] - dep[y]) {
ans = dep[i] - dep[y];
nowx = i;nowy = y;
}
}
else {
if(dep[i] + dep[y] - *dep[uy] > ans) {
ans = dep[i] + dep[y] - *dep[uy];
nowx = i;
nowy = y;
}
}
x= nowx;
y = nowy;
cout<<ans<<" ";
}
return ;
}

Codeforces 690 C3. Brain Network (hard) LCA的更多相关文章

  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. CF 690C3. Brain Network (hard) from Helvetic Coding Contest 2016 online mirror (teams, unrated)

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

  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. poj 3417 Network(tarjan lca)

    poj 3417 Network(tarjan lca) 先给出一棵无根树,然后下面再给出m条边,把这m条边连上,然后每次你能毁掉两条边,规定一条是树边,一条是新边,问有多少种方案能使树断裂. 我们设 ...

随机推荐

  1. 邮箱地址自动提示jQuery插件

    // mailAutoComplete.js v1.0 邮箱输入自动提示// 2010-06-18 v2.0 使用CSS class类代替CSS对象,同时增强代码可读性// 2010-06-18 v2 ...

  2. 对拍 bat命令快速模板

    对拍.bat @echo off :loop maker.exe > in.in wq.exe < in.in > out.out std.exe < in.in >st ...

  3. [Python3网络爬虫开发实战] 1.5.1-PyMySQL的安装

    在Python 3中,如果想要将数据存储到MySQL中,就需要借助PyMySQL来操作,本节中我们介绍一下它的安装方式. 1. 相关链接 GitHub:https://github.com/PyMyS ...

  4. 201621123082《Java程序设计》第1周学习总结

    1. 本周学习总结: 关键词: 了解Java语言的发展历史.了解Java语言的特点.JDK.JRE.JVM.eclipse等. 联系: JDK是提供给Java开发人员使用的一组工具,JDK包含JVM及 ...

  5. Project Euler

    Euler 34 答案:40730 我用程序算了无数次都是145,蛋疼,最后拿别人的程序仔细对比…… 原来 !=…… 真蛋疼,我竟然连基础数学都忘了 Euler-44 根据公式容易得出:Pmin + ...

  6. kubernetes 知识点及常用命令

    一.附上一个Deployment文件 apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: selec ...

  7. UvaLive 4863 Balloons(贪心)

    题意: 给定n个队伍, 然后A房间有a个气球, B房间有b个气球, 然后给出每个队伍所需要的气球数量和到A B房间的距离, 求把气球全部送到每个队伍的最短距离. 分析: 在气球充足的情况下, 那么我们 ...

  8. CUDA_one

    首先我看了讲解CUDA基础部分以后,大致对CUDA的基本了解如下: 第一:CUDA实行并行化的过程分为两部分,一个是线程块之间的并行(这是在每个线程网格中grid进行的),一个是对于每一个线程块内部各 ...

  9. sqlite3 新建数据库的过程

    有些东西,很简单,不过有坑,就变复杂了.我先说最简单的方法,新建一个空的txt文档,然后把后缀改为db就可以了.-_-蛋疼,其实一开始我是不知道的,也是后来成功新建db后发现db为0kb才大胆地做了这 ...

  10. stl lower_bound()和up_bound()

    iter=data.erase(iter);删掉 转载:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html STL中的每个算法 ...