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. Android-ViewPagerIndicator框架使用——CirclePageIndicator

    前言:Circle适用于应用新功能的展示页和商品的多张图片的展示功能. 1.定义布局文件:SampleCirclesDefault中添加了一个布局:simple_circles. 布局中定义一个Lin ...

  2. ios摇一摇功能

    在 UIResponder中存在这么一套方法 - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event __OSX_A ...

  3. sqlite3接口简要说明

    本文介绍一下SQLite C/C++接口. 早期的SQLite C/C++接口只有5个接口函数, 很容易学习. 新版本的SQLite新增了很多功能, 目前大概有185个API接口.本文介绍一些核心的A ...

  4. sql 触发器 针对一张表数据写入 另一张表 的增删改

    ALTER TRIGGER [dbo].[tri_test2] ON [dbo].[student] for INSERT,DELETE,UPDATEAS BEGIN if not exists (s ...

  5. 全国高校绿色计算大赛 预赛第一阶段(Python)

    第1关将字符串反转 #!/usr/bin/env python # -*- coding: utf-8 -*- class Task: def inversion(self, str): # **** ...

  6. ERC20 Token

    pragma solidity ^0.4.8; contract Token{ // token总量,默认会为public变量生成一个getter函数接口,名称为totalSupply(). uint ...

  7. [Python3网络爬虫开发实战] 2.2-网页基础

    用浏览器访问网站时,页面各不相同,你有没有想过它为何会呈现这个样子呢?本节中,我们就来了解一下网页的基本组成.结构和节点等内容. 1. 网页的组成 网页可以分为三大部分——HTML.CSS和JavaS ...

  8. Buffer.from()

    Buffer.from(array) array {Array} 使用一个8位字节的数组分配一个新的 Buffer. const buf = Buffer.from([0x62, 0x75, 0x66 ...

  9. 杭电1722 Cake (分蛋糕)

    #include<cstdio> int f(int m,int n) { ) return n; else return f(n,m%n); } int main() { int m,n ...

  10. Not so Mobile (针对递归输入的函数)

      Before being an ubiquous communications gadget, a mobile was just a structure made of strings and ...