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条边连上,然后每次你能毁掉两条边,规定一条是树边,一条是新边,问有多少种方案能使树断裂. 我们设 ...
随机推荐
- awk 对简单文本处理试水
#juanjuan是一个文件 [root@localhost c_test]# cat juanjuan , , bffd97d0 , , bffd97cc , , bffd97c8 , , #-F ...
- Laravel 控制器的request
public function request1(Request $request){ //取值 $name = Request::input('name'); //是否有值 if($request- ...
- haproxy和nginx负载均衡分析
https://my.oschina.net/zhuangweihong/blog/813231
- 系统重装 如何转换GPT的磁盘格式为MBR或者反过来
使用分区助手专业版可以让磁盘在GPT和MBR之间进行转换 一般把磁盘全部格式化并清除分区,剩下的都会是可用空间,还是需要重建MBR来把磁盘转换成MBR格式的 转换会设置操作系统类型
- Solidworks如何制作动画2
切换到Motion Study,然后定位到任意一帧,然后就可以摆弄当前装配体到新的位置和姿态,然后此时的时间和姿态就被记录下来了.以此类推可以多做几帧. 动画做好之后,点击播放可以预览.如果要保存,先 ...
- 图片3d轮放查看效果(V2.0):使用鼠标拖动实现图片的轮放
上面的版本号为通过左右button实现图片轮放,这个版本号.是通过在窗体拖动鼠标.左右滑动图片. 关键点在于选择一个合适的值.使鼠标拖动时.全部图片均可显示,可是不会滑动过快或离开窗体. 不多说,直接 ...
- HDU 2955 Robberies(01背包变形)
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- 关于cocos2d-x 3.0的点击交互处理
转自:http://blog.csdn.net/fansongy/article/details/12716671 1.概述 游戏也好,程序也好,仅仅有能与用户交互才有意义.手机上的交互大致能 ...
- Java Unit Testing - JUnit & TestNG
转自https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaUnitTesting.html yet another insignifican ...
- BingMap地图怎样显示中文
这是bingMap的js引用 <script type="text/javascript" src="v=7.0&mkt=zh-cn'></sc ...