CF 690C3. Brain Network (hard) from Helvetic Coding Contest 2016 online mirror (teams, unrated)
题目描述
Brain Network (hard) 这个问题就是给出一个不断加边的树,保证每一次加边之后都只有一个连通块(每一次连的点都是之前出现过的),问每一次加边之后树的直径。
算法
每一次增加一条边之后,树的直径长度要么不变,要么会增加1,并且如果树的直径长度增加1了,新的直径的端点其中一个必然是新增的点,而另一个是原来直径的某个端点。关于为什么可以这样做,在Quora上有个回答解释地不错,可以参考。
实现
所以这个问题其实就是要计算树上任意两个点的距离,LCA可以很轻松地处理。
可以一次性先把数据都读完,建树,因为每一次询问,后面加入的边不会对当前询问造成影响。
如果用二进制祖先那种搞法来算LCA的话,也可以每读一个新增点就去算一下,相当于是把原来的过程给拆开了。
下面是我的代码
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
const int N = 223456;
struct Edge {
int to, next;
} edge[N << 1];
int idx = 1, head[N];
void addEdge(int u, int v) {
edge[++idx].to = v;
edge[idx].next = head[u];
head[u] = idx;
}
void init() {
memset(head, 0, sizeof(head));
idx = 1;
}
int par[N][21];
int dep[N];
void dfs(int u, int fa, int d) {
dep[u] = d;
for (int k = head[u]; k; k = edge[k].next) {
int v = edge[k].to;
if (v == fa) continue;
par[v][0] = u;
dfs(v, u, d + 1);
}
}
void calc(int n) {
for (int i = 1; i <= 20; i++) {
for (int j = 1; j <= n; j++) {
par[j][i] = par[par[j][i - 1]][i - 1];
}
}
}
int kthA(int u, int k) {
for (int i = 20; i >= 0; i--) {
if (k >= (1 << i)) {
k -= (1 << i);
u = par[u][i];
}
}
return u;
}
int lca(int u, int v) {
if (dep[u] < dep[v])swap(u, v);
u = kthA(u, dep[u] - dep[v]);
if (u == v)return u;
for (int i = 20; i >= 0; i--) {
if (par[u][i] == par[v][i])continue;
u = par[u][i];
v = par[v][i];
}
return par[u][0];
}
bool update(int u, int v, int &k) {
int a = lca(u, v);
int d = dep[u] + dep[v] - 2 * dep[a];
if (d > k) {
k = d;
return true;
}
return false;
}
int a[N];
int main() {
int n;
scanf("%d", &n);
int u = 1, v = 1, cur = 0;
init();
for (int i = 2; i <= n; i++) {
scanf("%d", a + i);
addEdge(a[i], i);
}
dfs(1, -1, 0);
calc(n);
for (int i = 2; i <= n; i++) {
int nv = v;
if (update(u, i, cur)) {
nv = i;
}
if (update(v, i, cur)) {
u = v;
nv = i;
}
v = nv;
printf("%d ", cur);
}
puts("");
return 0;
}
CF 690C3. Brain Network (hard) from Helvetic Coding Contest 2016 online mirror (teams, unrated)的更多相关文章
- Helvetic Coding Contest 2016 online mirror C1
Description One particularly well-known fact about zombies is that they move and think terribly slow ...
- Helvetic Coding Contest 2016 online mirror A1
Description Tonight is brain dinner night and all zombies will gather together to scarf down some de ...
- Helvetic Coding Contest 2016 online mirror C2
Description Further research on zombie thought processes yielded interesting results. As we know fro ...
- Helvetic Coding Contest 2017 online mirror (teams allowed, unrated)
G. Fake News (easy) time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) J
Description Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their comm ...
- Helvetic Coding Contest 2016 online mirror F1
Description Heidi has finally found the mythical Tree of Life – a legendary combinatorial structure ...
- Helvetic Coding Contest 2016 online mirror B1
Description The zombies are gathering in their secret lair! Heidi will strike hard to destroy them o ...
- Helvetic Coding Contest 2016 online mirror D1
Description "The zombies are lurking outside. Waiting. Moaning. And when they come..." &qu ...
- Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) M
Description The marmots have prepared a very easy problem for this year's HC2 – this one. It involve ...
随机推荐
- python+request接口自动化框架
python+request接口自动化框架搭建 1.数据准备2.用python获取Excel文件中测试用例数据3.通过requests测试接口4.根据接口返回的code值和Excel对比 但本章只讲整 ...
- webpack 安装流程
我最近想看看wabpack,然后就面临着安装的问题,说实话,我一点也不懂cmd,怎么还需要用cmd安装呢.其实看教程上说可以在npm上安装,但是我打开npm总是出现闪退,所以我就选择了cmd 安装的过 ...
- css重要属性之辩
一.relative 1)与兄弟 relative对absolute 1.限制left/top/right/bottom定位 2.限制z-index层级: 3.限制在overflow下 relativ ...
- 2017年你需要一个VPN
还有29天就2017年了,今天跟同事还在讨论这个问题,2016你都做了些什么事情?2017你有什么计划和期待?有空我们一起来聊聊,今天我们就先来聊聊VPN. 记得2016年11月初的时候,我写过一篇文 ...
- Asp.net web api 知多少
本系列主要翻译自<ASP.NET MVC Interview Questions and Answers >- By Shailendra Chauhan,想看英文原版的可访问http:/ ...
- Python实现Windows定时关机
是最初的几个爬虫,让我认识了Python这个新朋友,虽然才刚认识了几天,但感觉有种莫名的默契感.每当在别的地方找不到思路,总能在Python找到解决的办法.自动关机,在平时下载大文件,以及跑程序的时候 ...
- JSP配置了虚拟目录使用JavaBean报错
今天遇到一个很棘手的问题,在jsp文件中使用useBean,网页返回码出现了500服务器内部错误,报错信息如下: The value for the useBean class attribute w ...
- Git的安装使用和基本命令(一)
版本控制系统是每一个程序员的必备神器,我相信任何一个程序员都要用到版本控制系统,它的强大之处我就不作解释了,在这我将Git(分布式版本控制系统)的安装使用和基本的命令给初学者介绍一下(在linux系统 ...
- 在调用相机后idleTimerDisabled失效的问题
在调用相机后idleTimerDisabled失效的问题 相关资料: http://stackoverflow.com https://github.com/jamiemcd 问题 前几天有人在群里边 ...
- node.js报错throw err; // Rethrow non-MySQL errors e:\serverTest\node_modules\mysql\lib\protocol\Parser.js:79 解决方法
今天在用node+angular做后台时,需要使用session保存登陆状态的时候,遇到了此问题,问题直译为非mysql问题,我也在后台取到的登陆用户名和密码,确实不是数据库问题.最后发现在使用ses ...