传送门啦

思路:

$ Lca $ 这个题要求这个显而易见吧。但是难就难在怎么在树上利用 $ Lca $ 去解决三个点的问题。

首先明确三个点两两的 三个 $ Lca $ 中有一对是相等的,我们也会发现这个相同的 $ Lca $ 肯定是深度最小的一个 $ Lca $ 。可以动手画一下图试试。

同样也用了一下树上差分的知识。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#define re register
using namespace std ;
const int maxn = 500005 ; inline int read () {
int f = 1 , x = 0 ;
char ch = getchar () ;
while(ch > '9' || ch < '0') {if(ch == '-') f = -1 ; ch = getchar () ;}
while(ch >= '0' && ch <= '9') {x = (x << 1) + (x << 3) + ch - '0' ; ch = getchar () ;}
return x * f ;
} int n , m , u , v , a , b , c ;
int head[maxn] , tot ; struct Edge {
int from , to , next ;
}edge[maxn << 1] ; inline void add (int u , int v) {
edge[++tot].from = u ;
edge[tot].to = v ;
edge[tot].next = head[u] ;
head[u] = tot ;
} int dep[maxn] , f[maxn][21] ; inline void dfs(int x , int fa) {
dep[x] = dep[fa] + 1 ;
f[x][0] = fa ;
for(re int i = 1 ; (1 << i) <= dep[x] ; ++ i) {
f[x][i] = f[f[x][i - 1]][i - 1] ;
}
for(re int i = head[x] ; i ; i = edge[i].next) {
int v = edge[i].to ;
if(v != fa) dfs(v , x) ;
}
} inline int lca(int a , int b) {
if(dep[a] < dep[b]) swap(a , b) ;
for(re int i = 20 ; i >= 0 ; -- i) {
if((1 << i) <= (dep[a] - dep[b])) {
a = f[a][i] ;
}
}
if(a == b) return a ;
for(re int i = 20 ; i >= 0 ; -- i) {
if((1 << i) <= dep[a] && (f[a][i] != f[b][i])) {
a = f[a][i] ;
b = f[b][i] ;
}
}
return f[a][0] ;
} int main () {
n = read () ; m = read () ;
for(re int i = 1 ; i <= n - 1 ; ++ i) {
u = read () ; v = read () ;
add(u , v) ;
add(v , u) ;
}
dfs(1 , 0) ;
for(re int i = 1 ; i <= m ; ++ i) {
a = read () ; b = read () ; c = read () ;
int root = 0 , ans = 0 ;
int r1 = lca(a , b) ;
int r2 = lca(b , c) ;
int r3 = lca(a , c) ;
if(r1 == r2) root = r3 ;
else if(r1 == r3) root = r2 ;
else root = r1 ;
ans = dep[a] + dep[b] + dep[c] - dep[r1] - dep[r2] - dep[r3] ;
printf("%d %d\n" , root , ans) ;
}
return 0 ;
}

洛谷P4281 紧急会议的更多相关文章

  1. 【题解】洛谷P4281 [AHOI2008] 紧急集合(求三个点LCA)

    洛谷P4281:https://www.luogu.org/problemnew/show/P4281 思路 答案所在的点必定是三个人所在点之间路径上的一点 本蒟蒻一开始的想法是:先求出2个点之间的L ...

  2. BZOJ1178或洛谷3626 [APIO2009]会议中心

    BZOJ原题链接 洛谷原题链接 第一个问题是经典的最多不相交区间问题,用贪心即可解决. 主要问题是第二个,求最小字典序的方案. 我们可以尝试从\(1\to n\)扫一遍所有区间,按顺序对每一个不会使答 ...

  3. 洛谷P4281 紧急集合 / 聚会

    LCA 题目要求找离三个点最近的点,我们先看两个点的情况,自然是找LCA,那么三个点的时候是否与LCA有关呢? 显然,离三个点最近的点一定是在这三个点联通的简单路径上. 可以简单证明一下,假设某个点离 ...

  4. 洛谷 P4281 [AHOI2008] 紧急集合 题解

    挺好的一道题,本身不难,就把求两个点的LCA变为求三个点两两求LCA,不重合的点才是最优解.值得一提的是,最后对答案的处理运用差分的思想:假设两点 一点深度为d1,另一点 深度为d2,它们LCA深度为 ...

  5. 洛谷P1395 会议(CODEVS.3029.设置位置)(求树的重心)

    To 洛谷.1395 会议 To CODEVS.3029 设置位置 题目描述 有一个村庄居住着n个村民,有n-1条路径使得这n个村民的家联通,每条路径的长度都为1.现在村长希望在某个村民家中召开一场会 ...

  6. 洛谷 P5044 - [IOI2018] meetings 会议(笛卡尔树+DP+线段树)

    洛谷题面传送门 一道笛卡尔树的 hot tea. 首先我们考虑一个非常 naive 的区间 DP:\(dp_{l,r}\) 表示区间 \([l,r]\) 的答案,那么我们考虑求出 \([l,r]\) ...

  7. 洛谷NOIp热身赛题解

    洛谷NOIp热身赛题解 A 最大差值 简单树状数组,维护区间和.区间平方和,方差按照给的公式算就行了 #include<bits/stdc++.h> #define il inline # ...

  8. 洛谷1640 bzoj1854游戏 匈牙利就是又短又快

    bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...

  9. 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.

    没有上司的舞会  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...

随机推荐

  1. 许仙章鱼TV

    http://v.youku.com/v_show/id_XMTY3NTYwNTE4MA==.html?from=s1.8-1-1.2&spm=a2h0k.8191407.0.0 201608 ...

  2. echarts图表点击事件之跳转页面和加载页面

    下图显示四个条形图,点击条形图就跳转到其页面,这说明您要判断你点了那个条形图. echarts给了它点击事件 写法,我们只要模仿就行,代码如下: //echarts图表点击跳转 myChart.on( ...

  3. 何谓SQL Server参数嗅探

    大家听到"嗅探"这个词应该会觉得跟黑客肯定有关系吧,使用工具嗅探一下参数,然后截获,脱裤o(∩_∩)o . 事实上,我觉得大家太敏感了,其实这篇文章跟数据库安全没有什么关系,实际上 ...

  4. django分页功能

    采用django自带的Paginator功能 from django.core.paginator import Paginator food = foodInfo.objects.filter(fo ...

  5. grafana worldPing插件

    worldPing插件安装 官网介绍:https://grafana.com/plugins/raintank-worldping-app/installation 插件下砸地址:https://gr ...

  6. 自己的Promise

    废话不多说,直接上代码: class Promise2{ constructor(fn){ const _this=this; //重点 this.__queue=[]; this.__succ_re ...

  7. 常用的css文件

    reset.css(几乎每个项目都要引入的css) @charset "utf-8";html{background-color:#fff;color:#000;font-size ...

  8. fastjson基本使用 (待继续完善)【原】

    参考: http://blog.csdn.net/wx_962464/article/details/37612861 maven库下载 fastjson基本样例1 Cat.java package ...

  9. 解决logstash启动缓慢问题

    在部署logstash时,头几次启动时长还可以,最后高达半小时以上启动启动不了,上网查资料说,系统的“熵”过低,导致jruby启动缓慢.需要安装haveged.但是我安装完后还是慢 https://h ...

  10. HDU - 3521 An easy Problem(矩阵快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=3521 题意 对于矩阵A,求e^A的值. 分析 这个定眼一看好像很熟悉,就是泰勒展开,可惜自己的高数已经还给老师了 ...