题目链接:

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 ;
}

最新文章

  1. juery学习总结(二)——juery操作页面元素
  2. PYTHON入门知识
  3. atitit.人脸识别的应用场景and使用最佳实践 java .net php
  4. 你能不用计算机来计算S=a+(a+1)+(a+2) + ...... + b的解的数目吗?
  5. javascript的执行顺序(转载)
  6. Node.js superagent 采集 URL 编码问题
  7. lua中打印所以类型功能实现table嵌套table
  8. 树后台数据存储(採用webmethod)
  9. iOS实现微信外部H5支付完成后返回原APP
  10. Executor, ExecutorService 和 Executors 间的区别与联系
  11. node全局安装说明(create-react-app、)
  12. Python Logging模块 输出日志颜色、过期清理和日志滚动备份
  13. Go学习入门
  14. JavaScript 删除数组中的对象
  15. java把html标签字符转换成普通字符(反转换成html标签)
  16. HDU1561:The more, The Better(树形DP+01背包)
  17. ES6系列_2之新的声明方式
  18. “Hello World!”团队第五周第五次会议
  19. svn &amp; git 问题汇总
  20. 安装 RabbitMQ (WINDOWS)

热门文章

  1. BZOJ——1614: [Usaco2007 Jan]Telephone Lines架设电话线
  2. ELK之Elasticsearch、logstash部署及配置
  3. 春哥的nginx systemtap调试脚本简单介绍
  4. delphi中Record 和Packed Record的区别
  5. 一道题目- Find the smallest range that includes at least one number from each of the k lists
  6. 全志a13开发总结
  7. TCP socket心跳包示例程序
  8. 【Mongodb教程 第十九课 】PHP与MONGODB的条件查询
  9. 【转载】分布式系统理论基础 - 一致性、2PC和3PC
  10. AOP和OOP的区别