题目链接:

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. Python入门--8--字符串

    一.创建.修改字符串 str1='呆呆 槑槑 木木 林林' str1[1] #输出呆 str1[2] #输出' ',也就是空值 str1=str[:5]+'插入乖呆 '+str1[5:] #修改字符串 ...

  2. jvisualvm远程监控 Visual GC plugin NOT supported for this JVM

    1. 找到jdk安装目录. 2. 进入jdk的 bin目录,新建文件jstatd.all.policy. 3.编辑jstatd.all.policy文件,内容如下: 4. 给jstatd.all.po ...

  3. Struts2的标签三大类是什么?

    Struts2 标签 一 Struts标签的简介: Struts2 自己封装了一套标签,比 JSTL 强大,而且与 Struts2 中的其他功能无缝结合. 当然 Strust2 标签的内容很多,随着版 ...

  4. 下载安装webstrom及激活

    太久没在新电脑上安装websrtom,又有点忘了咋激活. 一.安装 1.直接在浏览器搜索webstrom,打开官网,直接点击download.如下图 2.打开安装包,开始安装,直接点击 next 3. ...

  5. T1013 求先序排列 codevs

    http://codevs.cn/problem/1013/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Descr ...

  6. ubuntu下安装翻译软件

    原文: http://sixipiaoyang.blog.163.com/blog/static/6232358820144146386437/ Ubuntu下常用的翻译软件有StarDict,Gol ...

  7. 实现uitable cell中点击button设置当前cell为选中状态

    - (void)buttonClick:(id)senser{    NSInteger tag = [senser tag];    NSLog(@"the button tag is % ...

  8. influxdb的python操作

    1.先安装依赖:pip install influxdb 2.

  9. centos 7 卸載 mysql

    跟網上文章,安裝了一個mysqlwget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm 記下卸載過程: 首先执行查看命令 ...

  10. 课程的正确步调——Leo鉴书74

    <Leo鉴书(第1辑)>已登陆百度阅读.今后还将不断更新,免费下载地址:http://t.cn/RvawZEx 本人第一次站上讲台是1999年,那会儿从中关村回到天津,在一个给成人做计算机 ...