Codeforces 690 C3. Brain Network (hard) LCA
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
题意:
给你一个根节点1,之后每次加一条边,结点的父结点是树中已经得到的结点,问你加完边之后每一次的直径
题解:
新直径与加边之前的直径的关系,假设未加边之前是由X,Y这两点组成的链最长,那么答案必然是 max(dis(i,X),dis(i,Y),dis(X,Y));
这个画图作作假设就看得出
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
const int N=4e5+,mods=,inf=2e9+; int fa[N][],dep[N],n,f[N];
vector<int > G[N];
void Lca_dfs(int u,int p,int d) {
fa[u][] = p, dep[u] = d;
for(int i = ; i < G[u].size(); ++i) {
int to = G[u][i];
if(to == p) continue;
Lca_dfs(to,u,d+);
}
}
void Lca_init() {
Lca_dfs(,,);
for(int i = ; i <= ; ++i) {
for(int j = ; j <= n; ++j) {
if(fa[j][i-]) {
fa[j][i] = fa[fa[j][i-]][i-];
} else {
fa[j][i] = ;
}
}
}
}
int Lca(int x,int y) {
if(dep[x] > dep[y]) swap(x,y);
for(int k = ; k < ; ++k) {
if((dep[y] - dep[x])>>k&)
y = fa[y][k];
}
if(x == y) return x;
for(int k = ; k >= ; --k) {
if(fa[x][k] != fa[y][k]) {
x = fa[x][k];
y = fa[y][k];
}
}
return fa[x][];
}
int main() {
scanf("%d",&n);
for(int i = ; i <= n; ++i){
scanf("%d",&f[i]);
G[f[i]].push_back(i);
}
Lca_init();
int ans = ,x = ,y = ;
for(int i = ; i <= n; ++i) {
int ux = Lca(i,x);
int uy = Lca(i,y);
int nowx,nowy;
if(x == ux) {
if(ans < dep[i] - dep[x]) {
ans = dep[i] - dep[x];
nowx = i;nowy = x;
}
}else {
if(dep[i] + dep[x] - *dep[ux] > ans) {
ans = dep[i] + dep[x] - *dep[ux];
nowx = i;
nowy = x;
}
}
if(y == uy) {
if(ans < dep[i] - dep[y]) {
ans = dep[i] - dep[y];
nowx = i;nowy = y;
}
}
else {
if(dep[i] + dep[y] - *dep[uy] > ans) {
ans = dep[i] + dep[y] - *dep[uy];
nowx = i;
nowy = y;
}
}
x= nowx;
y = nowy;
cout<<ans<<" ";
}
return ;
}
Codeforces 690 C3. Brain Network (hard) LCA的更多相关文章
- codeforces 690C3 C3. Brain Network (hard)(lca)
题目链接: C3. Brain Network (hard) time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- 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条边连上,然后每次你能毁掉两条边,规定一条是树边,一条是新边,问有多少种方案能使树断裂. 我们设 ...
随机推荐
- 五、面向切面的spring(1)
spring的依赖注入看完了,接下来是spring中与DI一并重要的AOP了,开始吧,GO. 在软件开发中,散布于应用中多处的功能被称为横切发关注点,通常来讲,这些横切关注点从概念上市与应用的业务逻辑 ...
- nodejs初探
var http= require('http');var server= http.createServer(function(req,res){ res.writeHead(200,{" ...
- luogu 2257 YY的GCD
题目描述: 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对. 题解: 代码: #include<cstdio> # ...
- tomcat排错以及优化
jstack $PID #查看java进程的状态,分析tomcat卡死原因,定位java进程卡死的函数,调整代码 #RUNNABLE,在虚拟机内执行的.运行中状态,可能里面还能看到locked字样,表 ...
- codevs2597 团伙
题目描述 Description 1920年的芝加哥,出现了一群强盗.如果两个强盗遇上了,那么他们要么是朋友,要么是敌人.而且有一点是肯定的,就是: 我朋友的朋友是我的朋友: 我敌人的敌人也是我的朋友 ...
- 调用系统相机拍照,保存照片,调用系统裁剪API对照片处理,显示裁剪之后的照片
package com.pingyijinren.test; import android.annotation.TargetApi; import android.app.Notification; ...
- Delphi第三方控件安装方式
由于组件提供的方式不同,所以安装的方法也是不一样的,下面就目前常见的各种形式的组 件的安装方法介绍一下. 1只有一个DCU文件的组件.DCU文件是编译好的单元文件,这 ...
- Delphi与Windows 7下的用户账户控制(UAC)机制
WIN7, Vista提供的UAC机制,它的主要目的是防止对于操作系统本身的恶意修改.对于Delphi程序的影响,UAC主要在于以下几点:1.由于UAC机制,Delphi对于系统的操作可能无声的失败, ...
- SQLSERVER数据库管理员的专用连接DAC
出处: http://www.cnblogs.com/lyhabc/archive/2012/09/23/2698702.html DAC:Dedicated Admin Connection 当SQ ...
- hdu4701 Game(递推博弈)
题意: Alice初始有A元,Bob有B元. 有N个物品,第i个物品价值为Ci.Alice和Bob轮流买一些(>=1)物品.不能移动的人输.购买有一个限制,对于第1 个之后物品,只有当第i-1个 ...