Codeforces 690 C3. Brain Network (hard) LCA

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

612215

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+110,mods=20090717,inf=2e9+10;

int fa[N][30],dep[N],n,f[N];
vector<int > G[N];
void Lca_dfs(int u,int p,int d) {
    fa[u][0] = p, dep[u] = d;
    for(int i = 0; i < G[u].size(); ++i) {
        int to = G[u][i];
        if(to == p) continue;
        Lca_dfs(to,u,d+1);
    }
}
void Lca_init() {
    Lca_dfs(1,0,0);
    for(int i = 1; i <= 22; ++i) {
        for(int j = 1; j <= n; ++j) {
            if(fa[j][i-1]) {
                fa[j][i] = fa[fa[j][i-1]][i-1];
            } else {
                fa[j][i] = 0;
            }
        }
    }
}
int Lca(int x,int y) {
    if(dep[x] > dep[y]) swap(x,y);
    for(int k = 0; k < 22; ++k) {
        if((dep[y] - dep[x])>>k&1)
            y = fa[y][k];
    }
    if(x == y) return x;
    for(int k = 21; k >= 0; --k) {
        if(fa[x][k] != fa[y][k]) {
            x = fa[x][k];
            y = fa[y][k];
        }
    }
    return fa[x][0];
}
int main() {
    scanf("%d",&n);
    for(int i = 2; i <= n; ++i){
       scanf("%d",&f[i]);
       G[f[i]].push_back(i);
    }
    Lca_init();
    int ans = 0,x = 1,y = 1;
    for(int i = 2; 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] - 2*dep[ux] > ans) {
                ans = dep[i] + dep[x] - 2*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] - 2*dep[uy] > ans) {
                ans = dep[i] + dep[y] - 2*dep[uy];
                nowx = i;
                nowy = y;
            }
        }
        x=  nowx;
        y = nowy;
        cout<<ans<<" ";
    }
    return 0;
}
时间: 2025-01-04 03:01:36

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

G - Brain Network (easy)(并查集水题)

G - Brain Network (easy) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u CodeForces 690C1 Description One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don't know

【Codeforces 368A】Brain&#39;s Photos 水题

黑白灰都是#Black&White #include <cstdio> int n,m; int main() { scanf("%d%d",&n,&m); int ok=0; for(int i=0;i<n;i++) for(int j=0;j<m;j++) { char s[5]; scanf("%s",s); if(s[0]!='W'&&s[0]!='B'&&s[0]!='G')

codeforces 707A A. Brain&#39;s Photos(水题)

题目链接: A. Brain's Photos 题意: 问是黑白还是彩色; 思路: 没有思路: AC代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <bits/stdc++.h> #include <stack> #include <map> u

Brain Network (easy)

One particularly well-known fact about zombies is that they move and think terribly slowly. While we still don't know why their movements are so sluggish, the problem of laggy thinking has been recently resolved. It turns out that the reason is not (

codeforces 690C3 Brain Network

simple:并查集一下 #include <vector> #include <iostream> #include <queue> #include <cmath> #include <map> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; typedef long long LL; const

CodeForces 690C2 Brain Network (medium)(树上DP)

题意:给定一棵树中,让你计算它的直径,也就是两点间的最大距离. 析:就是一个树上DP,用两次BFS或都一次DFS就可以搞定.但两次的时间是一样的. 代码如下: #include<bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; vector<int> G[maxn]; int f[maxn], g[maxn], l[maxn]; int dfs(int root, int fa){ if(f[root] !=

CodeForces 690C1 Brain Network (easy) (水题,判断树)

题意:给定 n 条边,判断是不是树. 析:水题,判断是不是树,首先是有没有环,这个可以用并查集来判断,然后就是边数等于顶点数减1. 代码如下: #include <bits/stdc++.h> using namespace std; const int maxn =1000 + 5; int p[maxn]; int Find(int x){ return x == p[x] ? x : p[x] = Find(p[x]); } int main(){ int n, m, x, y; cin

complex brain network

Organization, development and function of complex brain networks The Brain as a Complex System: Using Network Science as a Tool for Understanding the Brain Rubinov M, Sporns O. Complex network measures of brain connectivity: uses and interpretations.

poj 3417 Network 【LCA】【树中增新边后 求每条树边被环所覆盖的次数】

Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4251   Accepted: 1223 Description Yixght is a manager of the company called SzqNetwork(SN). Now she's very worried because she has just received a bad news which denotes that DxtNet