CF832D题解
Description
给定一棵树上的三个点 \(a,b,c\),你要制定两条起点和终点都在这三个点中的路径,使得两条路径重叠的节点最多。
Solution
感觉我的方法和大众不同,显然是珂以Hack的
考虑分类讨论,分三类:
- \(a\) 点在这两条路径的起止点中出现 \(2\) 次。
- \(b\) 点在这两条路径的起止点中出现 \(2\) 次。
- \(c\) 点在这两条路径的起止点中出现 \(2\) 次。
下面只分析一类,即 \(a\) 出现两次(其实都一样的)。

如图,这时 \(\operatorname{LCA}(b,c)\) 不在 \(a\) 到 \(b\) 的简单路径上或\(\operatorname{LCA}(b,c)\) 不在 \(a\) 到 \(c\) 的简单路径上。
这就是说, \(\operatorname{LCA}(b,c)\) 不会被重复走过。
那么答案就是 \(\operatorname{dis}(a,\operatorname{LCA}(a,c))\) 和 \(\operatorname{dis}(a,\operatorname{LCA}(a,b))\) 中的最小值。(这个理解起来不难)

如图,这时 \(\operatorname{LCA}(b,c)\) 在 \(a\) 到 \(b\) 的简单路径上且\(\operatorname{LCA}(b,c)\) 在 \(a\) 到 \(c\) 的简单路径上。
这就是说, \(\operatorname{LCA}(b,c)\) 会被重复走过。
那么答案就是 \(\operatorname{dis}(a,\operatorname{LCA}(b,c))\) 。(这个理解起来更不难)
然后这题就愉快地做完了。
Code
#include<stdio.h>
#define reg register
#define ri reg int
#define rep(i, x, y) for(ri i = x; i <= y; ++i)
#define nrep(i, x, y) for(ri i = x; i >= y; --i)
#define DEBUG 1
#define INF 0x3fffffff
#define ll long long
#define il inline
#define swap(a, b) ((a) ^= (b) ^= (a) ^= (b))
#define max(i, j) (i) > (j) ? (i) : (j)
#define min(i, j) (i) < (j) ? (i) : (j)
#define read(i) io.READ(i)
#define print(i) io.WRITE(i)
#define push(i) io.PUSH(i)
struct IO {
#define MAXSIZE (1 << 20)
#define isdigit(x) (x >= '0' && x <= '9')
char buf[MAXSIZE], *p1, *p2;
char pbuf[MAXSIZE], *pp;
#if DEBUG
#else
IO() : p1(buf), p2(buf), pp(pbuf) {}
~IO() {
fwrite(pbuf, 1, pp - pbuf, stdout);
}
#endif
inline char gc() {
#if DEBUG
return getchar();
#endif
if(p1 == p2)
p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin);
return p1 == p2 ? ' ' : *p1++;
}
inline bool blank(char ch) {
return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
}
template <class T>
inline void READ(T &x) {
register double tmp = 1;
register bool sign = 0;
x = 0;
register char ch = gc();
for(; !isdigit(ch); ch = gc())
if(ch == '-') sign = 1;
for(; isdigit(ch); ch = gc())
x = x * 10 + (ch - '0');
if(ch == '.')
for(ch = gc(); isdigit(ch); ch = gc())
tmp /= 10.0, x += tmp * (ch - '0');
if(sign) x = -x;
}
inline void READ(char *s) {
register char ch = gc();
for(; blank(ch); ch = gc());
for(; !blank(ch); ch = gc())
*s++ = ch;
*s = 0;
}
inline void READ(char &c) {
for(c = gc(); blank(c); c = gc());
}
inline void PUSH(const char &c) {
#if DEBUG
putchar(c);
#else
if(pp - pbuf == MAXSIZE) {
fwrite(pbuf, 1, MAXSIZE, stdout);
pp = pbuf;
}
*pp++ = c;
#endif
}
template <class T>
inline void WRITE(T x) {
if(x < 0) {
x = -x;
PUSH('-');
}
static T sta[35];
T top = 0;
do {
sta[top++] = x % 10;
x /= 10;
} while(x);
while(top)
PUSH(sta[--top] + '0');
}
template <class T>
inline void WRITE(T x, char lastChar) {
WRITE(x);
PUSH(lastChar);
}
} io;
struct Edge {
int to, nxt, val;
} e[1000010];
int n, q, cnt, head[1000010], fath[1000010][22], dep[1000010], s, lg[500010];
void add(int u, int v, int val) {
e[++cnt].to = v;
e[cnt].nxt = head[u];
e[cnt].val = val;
head[u] = cnt;
}
void dfs(int now, int fa) {
dep[now] = dep[fa] + 1;
fath[now][0] = fa;
rep(i, 1, lg[dep[now]]) fath[now][i] = fath[fath[now][i - 1]][i - 1];
for(int i = head[now]; i; i = e[i].nxt) if(e[i].to != fa) dfs(e[i].to, now);
}
int lca(int x, int y) {
if(dep[x] < dep[y]) swap(x, y);
while(dep[x] > dep[y]) x = fath[x][lg[dep[x] - dep[y]] - 1];
if(x == y) return x;
nrep(i, lg[dep[x]] - 1, 0) if(fath[x][i] != fath[y][i]) x = fath[x][i], y = fath[y][i];
return fath[x][0];
}
int abs(int x) { return x < 0 ? -x : x; }
int dis(int x, int y) {
int l = lca(x, y);
return abs(dep[l] - dep[x]) + abs(dep[l] - dep[y]);
}
int check(int a, int b, int c) {
if(dis(a, c) + dis(b, c) == dis(a, b)) return 1;
return 0;
}
int solve(int a, int b, int c) {
if(check(a, b, lca(b, c)) && check(a, c, lca(b, c))) return dis(a, lca(b, c));
return min(dis(a, lca(a, b)), dis(a, lca(a, c)));
}
int main() {
read(n), read(q);
rep(i, 2, n) {
int x;
read(x);
add(x, i, 1), add(i, x, 1);
}
rep(i, 1, n) lg[i] = lg[i - 1] + (1 << lg[i - 1] == i);
dfs(1, 0);
rep(i, 1, q) {
int x, y, z;
read(x), read(y), read(z);
int ans = max(solve(x, y, z), max(solve(y, x, z), solve(z, x, y)));
printf("%d\n", ans);
}
return 0;
}
CF832D题解的更多相关文章
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
- 网络流n题 题解
学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...
- CF100965C题解..
求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...
随机推荐
- vue项目中一些标签直接放在<template>下会报错Failed to compile with 1 errors
原因是a标签button以及element-ui的组件不能直接放在<template>下,需要先有一个div,其他标签要放在div下
- vsftpd.conf配置文件详解
1.默认配置: 1>允许匿名用户和本地用户登陆. anonymous_enable=YES local_enable=YES 2>匿名用户使用的登陆名为ftp或anonymous,口令为空 ...
- 4.13、nfs挂载优化及优缺点
1.硬盘:sas/ssd磁盘,买多块,硬件raid5/raid0,网卡吞吐量要大,至少千兆(多网卡bond0) 2.nfs客户端挂载说明: 文件系统有自己的权限,挂载是建立在文件系统之上的,然后更改挂 ...
- 选择适合小企业的CRM软件
随着信息时代的到来和客户掌握的信息变多,大多数企业开始从"以产品为中心"转变为"以客户为中心".为了适应市场的变化,许多企业开始使用客户关系管理软件来提高工作效 ...
- Swoole实现毫秒级定时任务
项目开发中,如果有定时任务的业务要求,我们会使用linux的crontab来解决,但是它的最小粒度是分钟级别,如果要求粒度是秒级别的,甚至毫秒级别的,crontab就无法满足,值得庆幸的是swoole ...
- 基于Vue/React项目的移动端适配方案
本文的目标是通过下文介绍的适配方案,使用vue或react开发移动端及H5的时候,不需要再关心移动设备的大小,只需要按照固定设计稿的px值布局,提升开发效率. 下文给出了本人分别使用create-re ...
- http、tcp和socket简单理解
1.Http属于应用层,主要解决如何包装数据. 2.Tcp属于传输层,主要解决数据如何在网络上传输. 3.Socket是对TCP/IP协议的封装,Socket本身并不是协议,而是一个调用接口(API) ...
- 远程cmd操作
<<PSTools.zip>><<Install_PowerCmd.exe>><<cmder_mini.zip>><< ...
- python找出字典中value最大值的几种方法
假设定义一字典,m = {"a":3,"e":6,"b":2,"g":7,"f":7,"c ...
- Selenium自动化测试框架Ride使用XLRD对于Excel测试数据的管理和操作
Python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库. 一.安装xlrd模块 到python官网下载http://pypi.pytho ...