题目链接:

C3. Brain Network (hard)

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 

题意:

给一棵树的生成过程,问在每次添加一个节点后这棵树的直径是多少;

思路:

在新加的一个节点w前的直径是(u,v),加入w后,直径就变成了max{(u,v)(u,w)(v,w)};
然后就是把lca约束成RMQ问题来了; AC代码:
#include <bits/stdc++.h>
/*
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
*/
using namespace std;
#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=2e5+;
const int maxn=;
const double eps=1e-; int n,in[N],a[*N],dep[N],cnt=,dp[*N][],dis[N];
vector<int>ve[N]; void dfs(int x,int deep)
{
//cout<<x<<" "<<deep<<endl;
in[x]=cnt;
a[cnt++]=x;
dep[x]=deep;
int len=ve[x].size();
For(i,,len-)
{
int y=ve[x][i];
dis[y]=dis[x]+;
dfs(y,deep+);
a[cnt++]=x;
}
} int RMQ()
{
for(int i=;i<cnt;i++)
dp[i][]=a[i]; for(int j=;(<<j)<=cnt;j++)
{
for(int i=;i+(<<j)-<cnt;i++)
{
if(dep[dp[i][j-]]<dep[dp[i+(<<(j-))][j-]])dp[i][j]=dp[i][j-];
else dp[i][j]=dp[i+(<<(j-))][j-];
}
}
}
int query(int l ,int r)
{
if(l>r)swap(l,r);
int temp=(int)(log((r-l+)*1.0)/log(2.0));
if(dep[dp[l][temp]]<dep[dp[r-(<<temp)+][temp]])return dp[l][temp];
return dp[r-(<<temp)+][temp];
}
int s=,e=,pre=;
int check(int x,int y)
{
//cout<<x<<" "<<y<<" "<<pre<<" @@@@"<<endl;
int temp=query(in[x],in[y]);
if(dis[x]+dis[y]-*dis[temp]>pre)
{
s=x;
e=y;
pre=dis[x]+dis[y]-*dis[temp];
}
} int main()
{
read(n);
int u;
For(i,,n)
{
read(u);
ve[u].push_back(i);
}
dis[]=;
dfs(,);
RMQ();
For(i,,n)
{
int fs=s,fe=e;
check(fs,fe);
check(fs,i);
check(fe,i);
cout<<pre<<" ";
} return ;
}

codeforces 690C3 C3. Brain Network (hard)(lca)的更多相关文章

  1. Codeforces 690 C3. Brain Network (hard) LCA

    C3. Brain Network (hard)   Breaking news from zombie neurology! It turns out that – contrary to prev ...

  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. AFNetworking实时监测网络连接

    
// 网络变化消息 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(checkNetwork) ...

  2. ftrace 提供的工具函数

    内核头文件 include/linux/kernel.h 中描述了 ftrace 提供的工具函数的原型,这些函数包括 trace_printk.tracing_on/tracing_off 等.本文通 ...

  3. Netty构建游戏服务器(三)--netty spring简单整合

    一,基本方法 上节实现了netty的基本连接,这节加入spring来管理netty,由spring来开启netty服务. 在netty服务器中,我们建立了三个类:HelloServer(程序主入口) ...

  4. 微服务网关实战——Spring Cloud Gateway

    导读 作为Netflix Zuul的替代者,Spring Cloud Gateway是一款非常实用的微服务网关,在Spring Cloud微服务架构体系中发挥非常大的作用.本文对Spring Clou ...

  5. transition、animation在macbook air上图片动画边缘抖动2

    示例: BUG描述: 最近同事一项目中,产品提出在macbook air上,列表图片放大效果边缘出现抖动现象.在retina屏上没有此问题. 调试过程: 1.单独把结构分离.确定是否由其他元素引起. ...

  6. How to Install a Language Pack

    https://www.phpbb.com/kb/article/how-to-install-a-language-pack

  7. 全志a13开发总结

    这几天因为工作的原因,開始接触全志a13芯片,本人在网上搜集了好长时间,可是网上的资料对这方面的描写叙述是很少的, 所以,仅仅能靠数据手冊还有官网上面的英文文档进行开发了,下面仅仅是开发中的非常少的一 ...

  8. Qt:解析命令行

    Qt从5.2版開始提供了两个类QCommandLineOption和QCommandLineParser来解析应用的命令行參数. 一.命令行写法 命令行:"-abc" 在QComm ...

  9. Redis经常使用命令

    1 创建-是否存储-查看-删除 set name maojun;exists name;get name;del name; 2 序列化记录 set name maojun;exists name;d ...

  10. MYSQL时间戳的处理

    date为需要处理的参数(该参数是Unix 时间戳),可以是字段名,也可以直接是Unix 时间戳字符串 后面的 '%Y%m%d' 主要是将返回值格式化 例如: mysql>SELECT FROM ...