传送门

可以看出,三个点两两之间的lca会有一对相同,而另一个lca就是聚集点。

然后搞搞就可以求出距离了。

——代码

 #include <cstdio>
#include <cstring>
#include <iostream>
#define MAXN 1000001 using namespace std; int n, m, cnt, ans;
int head[MAXN], to[MAXN], next[MAXN], deep[MAXN], f[MAXN][]; inline void add(int x, int y)
{
to[cnt] = y;
next[cnt] = head[x];
head[x] = cnt++;
} inline void dfs(int u)
{
int i, v;
deep[u] = deep[f[u][]] + ;
for(i = ; f[u][i]; i++) f[u][i + ] = f[f[u][i]][i];
for(i = head[u]; i != -; i = next[i])
{
v = to[i];
if(!deep[v]) f[v][] = u, dfs(v);
}
} inline int lca(int x, int y)
{
int i;
if(deep[x] < deep[y]) swap(x, y);
for(i = ; i >= ; i--)
if(deep[f[x][i]] >= deep[y])
x = f[x][i];
if(x == y) return x;
for(i = ; i >= ; i--)
if(f[x][i] != f[y][i])
x = f[x][i], y = f[y][i];
return f[x][];
} int main()
{
int i, x, y, z, a;
scanf("%d %d", &n, &m);
memset(head, -, sizeof(head));
for(i = ; i < n; i++)
{
scanf("%d %d", &x, &y);
add(x, y);
add(y, x);
}
dfs();
for(i = ; i <= m; i++)
{
scanf("%d %d %d", &x, &y, &z);
a = lca(x, y) ^ lca(x, z) ^ lca(y, z);
ans = deep[x] + deep[a] - * deep[lca(x, a)];
ans += deep[y] + deep[a] - * deep[lca(y, a)];
ans += deep[z] + deep[a] - * deep[lca(z, a)];
printf("%d %d\n", a, ans);
}
return ;
}

[bzoj1787][Ahoi2008]Meet 紧急集合(lca)的更多相关文章

  1. BZOJ1787 [Ahoi2008]Meet 紧急集合 LCA

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1787 题意概括 有一棵节点为n个(n≤500000)的树.接下来m次询问(m≤500000),每次 ...

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

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

  3. 【BZOJ1787】[Ahoi2008]Meet 紧急集合 LCA

    [BZOJ1787][Ahoi2008]Meet 紧急集合 Description Input Output Sample Input 6 4 1 2 2 3 2 4 4 5 5 6 4 5 6 6 ...

  4. BZOJ1787 [Ahoi2008]Meet 紧急集合 【LCA】

    1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec  Memory Limit: 162 MB Submit: 3578  Solved: 1635 [Submi ...

  5. bzoj1787 [Ahoi2008]Meet 紧急集合

    1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec  Memory Limit: 162 MB Submit: 2272  Solved: 1029 [Submi ...

  6. BZOJ 1787: [Ahoi2008]Meet 紧急集合 LCA

    1787: [Ahoi2008]Meet 紧急集合 Description Input Output Sample Input 6 4 1 2 2 3 2 4 4 5 5 6 4 5 6 6 3 1 ...

  7. 【块状树】【LCA】bzoj1787 [Ahoi2008]Meet 紧急集合

    分块LCA什么的,意外地快呢…… 就是对询问的3个点两两求LCA,若其中两组LCA相等,则答案为第三者. 然后用深度减一减什么的就求出距离了. #include<cstdio> #incl ...

  8. BZOJ1787 [Ahoi2008]Meet 紧急集合[结论题]

    location. 求到树上三点距离和最短的点及此距离. 这个不还是分类讨论题么,分两类大情况,如下图. 于是乎发现三个点对的lca中较深的那个lca是答案点.距离就是两两点对距离加起来除以2即可.这 ...

  9. LCA 【bzoj1787】[Ahoi2008]Meet 紧急集合

    LCA [bzoj1787][Ahoi2008]Meet 紧急集合 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1787 注意到边权为一 ...

随机推荐

  1. qconbeijing2015

    http://2015.qconbeijing.com/schedule 大会日程 2015年4月23日,星期四 地点 2号厅 203AB 201AB 9:15 开场致辞 专题 主题演讲 互联网金融背 ...

  2. winform 打印小票

    后台代码 panPrintContent.Visible = true; var strlPrinterMode = ""; ; ; ; ; panPrintContent.Vis ...

  3. AJPFX实现兔子问题

    有一对小兔子,从第三个月长成开始每个月生一对小兔子,新出生的小兔子从第三个月长成开始每个月也生一对小兔子,假设所有的兔子都不会死,问每个月兔子的总数?(月数可以是6,12).大神看看我笨方法谢的对吗? ...

  4. expect下命令不能解析通配符*的问题

    曾遇到这样一段代码:(Bash脚本) 1 2 3 4 5 6 7 8 9 10 11 12 #!/usr/bin/expect -f set HOST "192.168.102.1" ...

  5. spark性能优化-JVM虚拟机垃圾回收调优

    1 2 3 4

  6. PHP实现远程图片下载

    /** * 文件下载 * @param string $url */ public function download() { $url = $this->input->get_post( ...

  7. Outlook 数据文件(.pst 和 .ost)简介

    使用 Microsoft Outlook 时,电子邮件.日历.任务和其他项目保存在邮件服务器或计算机上,或者同时保存在这两个位置.如果 Outlook 项目保存在计算机上,则它们保存在 Outlook ...

  8. Python 使用random模块生成随机数

    需要先导入 random  模块,然后通过 random 静态对象调用该一些方法. random() 函数中常见的方法如下: # coding: utf-8 # Team : Quality Mana ...

  9. Vector(同步)和ArrayList(异步)异同

    //1 同步 异步 //未响应 = 假死 占用内存过多 内存无法进行处理 //请求的方式 :同步 异步 //网页的展现过程中(同步):1. css文件的下载 2.js文件的下载 3.下载你当前的htm ...

  10. PHP03 移动互联网和PHP

    学习要点 移动互联网 云计算 网络通信协议 Apache http服务器 PHP运行原理 学习目标 理解网络通信协议 掌握PHP运行原理 WAMP开发环境的搭建   移动互联网 定义 移动互联网,就是 ...