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 ui创建的项目怎么关闭eslint校验
在Vue Cli的控制面板找到配置-ESLint configuration,然后关闭保存时检查就可以了
- 大白话spring依赖注入
在前边的文章中分享了spring如何实现属性的注入,有注解和配置文件两种方式,通过这两种方式可以实现spring中属性的注入,具体配置可查看<spring入门(一)[依赖注入]>,那么sp ...
- Redmine部署中遇到的问题
Redmine部署文章: 第一篇:Redmine部署 第二篇:Redmine部署中遇到的问题 上一篇文章我写了Redmine怎样部署(点这里直达上一篇文章),这一篇就写一下在Redmine部署中遇到过 ...
- Qt:报文接收不完成,产生分帧的处理方法
最近在设备的测试工装时,通过串口,向设备发送自定义规约,其报文的枕结构已经编写,使用串口助手调试,设备可正常回复,但是通过工装,接收报文会不完整,导致解析失败.使用qDebug打印出来却发现数据被分成 ...
- excel函数提取内容中的汉字
RIGHT(A2,LENB(A2)-LEN(A2)) 函数LENB将每个汉字(双字节字符)的字符数按2计数,LEN函数则对所有的字符都按1计数.因此"LENB(A2)-LEN(A2)&quo ...
- GCP消息队列Pubsub详解,简单好用还不用自己运维
我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 简介 GCP的Pubsub是一种异步消息传递服务,可将生产事件的服务与处理事件的服务隔离开.消息队列的作用就不多作介绍 ...
- 乘风破浪,Windows11官方原装4K壁纸,前卫的艺术数字设计
Windows11预览版官方壁纸 默认主题Windows Windows.zip 月轮主题ThemeA ThemeA.zip 艺术石主题ThemeB ThemeB.zip 日升主题ThemeC The ...
- .net获取项目根目录方法集合
这篇文章是别的博客复下来,收藏的: 编写程序的时候,经常需要用的项目根目录.自己总结如下 1.取得控制台应用程序的根目录方法 方法1.Environment.CurrentDirectory ...
- UVA 11475 Extend to Palindrome hash
题意: 给出一个字符串,让你往后添加最少的字符,使其成为回文串. 分析: 题目就相当于求后缀字符串为回文串的最长长度,判断回文串要O(n)时间,直接判断肯定不行.我们从后往前枚举,每次字符串与上一个字 ...
- WebService:CXF的JaxWsDynamicClientFactory实现调用WebService接口
首先需要引入依赖jar包 #版本只供参考,具体看项目 <dependency> <grouId>org.apache.cxf</grouId> <artifa ...