codeforces 690C3 C3. Brain Network (hard)(lca)
题目链接:
2 seconds
256 megabytes
standard input
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.
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 n - 1 space-separated numbers – the brain latencies after the brain number k is added, for k = 2, 3, ..., n.
6
1
2
2
1
5
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)的更多相关文章
- Codeforces 690 C3. Brain Network (hard) LCA
C3. Brain Network (hard) Breaking news from zombie neurology! It turns out that – contrary to prev ...
- codeforces 690C2 C2. Brain Network (medium)(bfs+树的直径)
题目链接: C2. Brain Network (medium) time limit per test 2 seconds memory limit per test 256 megabytes i ...
- codeforces 690C1 C1. Brain Network (easy)(水题)
题目链接: C1. Brain Network (easy) time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- CF 690C3. Brain Network (hard) from Helvetic Coding Contest 2016 online mirror (teams, unrated)
题目描述 Brain Network (hard) 这个问题就是给出一个不断加边的树,保证每一次加边之后都只有一个连通块(每一次连的点都是之前出现过的),问每一次加边之后树的直径. 算法 每一次增加一 ...
- Brain Network (medium)(DFS)
H - Brain Network (medium) Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d &am ...
- Brain Network (easy)(并查集水题)
G - Brain Network (easy) Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- Brain Network (medium)
Brain Network (medium) Further research on zombie thought processes yielded interesting results. As ...
- Brain Network (easy)
Brain Network (easy) One particularly well-known fact about zombies is that they move and think terr ...
- poj 3417 Network(tarjan lca)
poj 3417 Network(tarjan lca) 先给出一棵无根树,然后下面再给出m条边,把这m条边连上,然后每次你能毁掉两条边,规定一条是树边,一条是新边,问有多少种方案能使树断裂. 我们设 ...
随机推荐
- Lucene 6.5.0 入门Demo(2)
参考文档:http://lucene.apache.org/core/6_5_0/core/overview-summary.html#overview.description 对于path路径不是很 ...
- CentOS 7.5 安装Docker 教程
Docker简介 Docker是一个开源的容器引擎,它有助于更快地交付应用.Docker可将应用程序和基础设施层隔离,并且能将基础设施当作程序一样进行管理. 使用Docker可更快地打包.测试以及部署 ...
- ELK之Elasticsearch、logstash部署及配置
ElasticSearch是一个搜索引擎,用来搜索.分析.存储日志; Logstash用来采集日志,把日志解析为json格式交给ElasticSearch; Kibana是一个数据可视化组件,把处理后 ...
- CODEVS_2800 送外卖 状态压缩+动态规划
原题链接:http://codevs.cn/problem/2800/ 题目描述 Description 有一个送外卖的,他手上有n份订单,他要把n份东西,分别送达n个不同的客户的手上.n个不同的客户 ...
- sqlite 常用操作
#查看当前数据库信息 .database #列出所有表 .tables #列出所有字段 .schema 或者 .schema table_name #清空一张表 delete from tabl ...
- Java搜索引擎选择: Elasticsearch与Solr(转)
Elasticsearch简介 Elasticsearch是一个实时的分布式搜索和分析引擎.它可以帮助你用前所未有的速度去处理大规模数据. 它可以用于全文搜索,结构化搜索以及分析,当然你也可以将这三者 ...
- 如何删除xcode启动主页面项目列表
Open Xcode, leave the splash screen up and choose "File", "Open Recent Projects" ...
- 辅助方法 @Html.Raw与 HtmlString区别
//Html.Raw其实是调用 new Microsoft.AspNetCore.Html.HtmlString(xxx) @{ ViewData["Title"] = " ...
- 删除DataGridView选中行并更新数据库
前面写过一篇文章是DataGridView控件显示数据的,DataGridView在与数据库打交道时会常常出现,也非常有用.通过DataGridView对数据库进行更改和查询都比較方便. 这里我们须要 ...
- odoo写邮件添加收件人
在任何可以写消息的地方点击鼠标 或者回复消息 写消息的框会聚焦并变大 点击撰写框右上角的弹出窗图标 弹出完整的撰写消息窗口 在红色的地方添加收件 ...