题目描述

这次也是很长的题面啊\(qwq\)

题目大意如下:

给定一棵\(N\)个节点的树以及\(M\)次询问,每次询问给出\(x,\ y,\ z\)三个节点,程序需要在树上找一个点\(p\)

使得\(c = dist(x,p)+dist(y,p)+dist(z,p)\)取最小值,每一次询问输出满足条件的\(p\)和此时的最小的\(c\)


基本思路

看到树上距离的题,很容易想到\(LCA\),但是此处有三个点,不能直接用\(LCA\),所以我们得绕一点弯...

考虑求出三个点两两之间的$LCA,\ $那么我们可以马上想到:

\[p \in \{ LCA(x,y),LCA(x,z),LCA(y,z)\}
\]

证明:

对于一条树上路径\((x, y)\),显然对于

\[\forall\ p \in (x,y), dist(x,p)+dist(p,y)=dist(x,y)
\]

所以该$\ p\ \(点并不会影响\)x,y\(的费用,所以我们应该尽可能使\)\ p\ \(点靠近\)\ z\ $点

此时需要分类讨论 :

  1. \(z \in (x,y) ,\)那么\(p=z\)
  2. \(z \notin (x,y),\)那么
    1. \(z \in SubTree(x)(\)或 \(z \in SubTre(y)),p=x(\)或\(p=y)\)
    2. \(z \notin SubTree(x)\)且\(z \notin SubTree(y), z=LCA(x,y)\)

是不是感觉好麻烦?其实我们完全没必要做这么多。\(qwq\)

因为\(x,y,z\)是有轮换性的,所以只需要用某两个点之间的\(LCA\)试着更新\(p\)点就好了,结论也就是这么出来的

(具体过程可以根据上面的分类讨论,自己yy一下,最好画一张图我才不会告诉你我不会用几何画板)


细节注意事项

计算树上路径时,一定不要直接用\(dep\)去减,跑得过样例但是会WA


参考代码

#include <cstdio>
#include <cstring>
#define rg register
const int MAXN = 500010;
inline int abs(int a) { return a < 0 ? -a : a; }
inline void swap(int& a, int& b) { int t = a; a = b; b = t; }
inline int read() {
int s = 0; bool f = false; char c = getchar();
while (c < '0' || c > '9') f |= (c == '-'), c = getchar();
while (c >= '0' && c <= '9') s = (s << 3) + (s << 1) + (c ^ 48), c = getchar();
return f ? -s : s;
}
int tot, head[MAXN], nxt[MAXN << 1], ver[MAXN << 1];
inline void Add_edge(int u, int v) { nxt[++tot] = head[u], head[u] = tot, ver[tot] = v; }
int dep[MAXN], f[MAXN][22];
inline void dfs(int u, int fa) {
dep[u] = dep[fa] + 1, f[u][0] = fa;
for (rg int i = 1; i <= 20; i++)
f[u][i] = f[f[u][i - 1]][i - 1];
for (rg int v, i = head[u]; i; i = nxt[i])
if(!dep[v = ver[i]]) dfs(v, u);
}
inline int LCA(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (rg int i = 20; ~i; --i) if (dep[f[x][i]] >= dep[y]) x = f[x][i];
if (x == y) return x;
for (rg int i = 20; ~i; --i) if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
return f[x][0];
}
int main() {
int n = read(), q = read();
for (rg int i = 1; i < n; i++) {
int u = read(), v = read();
Add_edge(u, v), Add_edge(v, u);
}
dfs(1, 0);
for (rg int i = 1; i <= q; i++) {
int x = read(), y = read(), z = read();
int pos, minn = 2147483647, c;
//根据轮换性(所以这三段都长得差不多...)
int lca1 = LCA(x, y);
c = dep[x] + dep[y] - 2 * dep[lca1];
int lcaz = LCA(lca1, z);
c += dep[lca1] + dep[z] - 2 * dep[lcaz];
if (minn > c) pos = lca1, minn = c; int lca2 = LCA(x, z);
c = dep[x] + dep[z] - 2 * dep[lca2];
int lcay = LCA(lca2, y);
c += dep[lca2] + dep[y] - 2 * dep[lcay];
if (minn > c) pos = lca2, minn = c; int lca3 = LCA(y, z);
c = dep[y] + dep[z] - 2 * dep[lca3];
int lcax = LCA(lca3, x);
c += dep[x] + dep[lca3] - 2 * dep[lcax];
if (minn > c) pos = lca3, minn = c; printf("%d %d\n", pos, minn);
}
return 0;
}

完结撒花\(qwq\)

「AHOI2008」紧急集合/聚会的更多相关文章

  1. 「AHOI2008」「LuoguP4281」紧急集合 / 聚会(LCA

    题目描述 欢乐岛上有个非常好玩的游戏,叫做“紧急集合”.在岛上分散有N个等待点,有N-1条道路连接着它们,每一条道路都连接某两个等待点,且通过这些道路可以走遍所有的等待点,通过道路从一个点到另一个点要 ...

  2. 【bzoj1787】&【bzoj1832】[Ahoi2008]Meet 紧急集合 & 聚会

    bzoj1787就是bzoj1832 bzoj1832 空间和时间少了一些... 求三个结点到一个结点距离之和最小的结点以及距离和 求出两两lca,其中有两个相同,答案则为另一个 感觉就是一大暴力.. ...

  3. P4281 [AHOI2008]紧急集合 / 聚会

    P4281 [AHOI2008]紧急集合 / 聚会 lca 题意:求3个点的lca,以及3个点与lca的距离之和. 性质:设点q1,q2,q3 两点之间的lca t1=lca(q1,q2) t2=lc ...

  4. [AHOI2008]紧急集合 / 聚会(LCA)

    [AHOI2008]紧急集合 / 聚会 题目描述 欢乐岛上有个非常好玩的游戏,叫做"紧急集合".在岛上分散有N个等待点,有N-1条道路连接着它们,每一条道路都连接某两个等待点,且通 ...

  5. bzoj1787[Ahoi2008]Meet 紧急集合&bzoj1832[AHOI2008]聚会

    bzoj1787[Ahoi2008]Meet 紧急集合 bzoj1832[AHOI2008]聚会 题意: 给个树,每次给三个点,求与这三个点距离最小的点. 题解: 倍增求出两两之间的LCA后,比较容易 ...

  6. bzoj 1787 [Ahoi2008]Meet 紧急集合(1832 [AHOI2008]聚会)

    1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 1841  Solved: 857[Submit][ ...

  7. 【BZOJ-1787&1832】Meet紧急集合&聚会 倍增LCA

    1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 2259  Solved: 1023[Submit] ...

  8. 「译」JUnit 5 系列:条件测试

    原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...

  9. 「译」JUnit 5 系列:扩展模型(Extension Model)

    原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...

随机推荐

  1. rg.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'TL_C_CONS_ExtendController':

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'TL_ ...

  2. memcache安装 基于Red Hat 7.4

    Linux版本为 Red Hat 7.4 一.安装 1.下载:http://memcached.org/downloads 解压: tar -xzvf memcached-1.5.3.tar.gz 得 ...

  3. docker安装后启动报错

    docker安装后启动不起来: 查看日志  /var/log/message    其中有一行为:  Your kernel does not support cgroup memory limit ...

  4. cmake 单个目录多个文件的情况

    参考:https://www.hahack.com/codes/cmake/# 源文件一共有三个:main.cpp.MathFunctions.h.MathFunctions.cpp 文件内容分别如下 ...

  5. Idea 工具快捷合集

    官方下载地址 https://www.jetbrains.com/idea/download/#section=windows 商业版 与 社区版,商业版具有更多的功能 快捷一.修改 terminal ...

  6. 学习笔记(26)- plato-端到端模型-定闹钟

    今天用了定闹钟的场景语料,在plato框架尝试了端到端的模型. 本文先记录英文的训练过程,然后记录中文的训练过程. 训练端到端的模型 发现使用英文的模型,还是显示有中文,所以,新建目录,重新训练 1. ...

  7. sqlite3 install 和使用

    windows: 在 Windows 上安装 SQLite 请访问 http://www.sqlite.org/download.html,从 Windows 区下载预编译的二进制文件. 您需要下载  ...

  8. CAS 和 ABA 问题

    CAS简介 CAS 全称是 compare and swap,是一种用于在多线程环境下实现同步功能的机制. CAS 它是一条CPU并发原语.操作包含三个操作数 -- 内存位置.预期数值和新值.CAS ...

  9. Plastic Sprayers Manufacturer - Ingenious Design Of Spray Plastic Bottle

    Plastic bottles are now an indispensable container in life. Plastic bottles will appear in all aspec ...

  10. javascript数据类型及类型的转换总结

    javascript 是浏览器客户端脚本语言,要想让网页与后台程序更好的交互效果,这里我们详细了解javascript 数据类型及类型的转换 1,数据类型 number number类型 数字类型,浮 ...