传送门

可以看出,三个点两两之间的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. Elasticsearch (2) - 映射

    常用映射类型 核心的字段类型如下: String 字符串包括text和keyword两种类型: 1.text analyzer 通过analyzer属性指定分词器. 下边指定name的字段类型为tex ...

  2. 【转】数据库CRUD操作

    数据库CRUD操作 一.删除表   drop table 表名称 二.修改表 alter   table 表名称 add  列名 数据类型   (add表示添加一列) alter  table  表名 ...

  3. 51nod 1417 天堂里的游戏

    基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 多年后,每当Noder看到吉普赛人,就会想起那个遥远的下午. Noder躺在草地上漫无目的的张望,二 ...

  4. ArrayList Vector LinkedList分析

    1.创建 ArrayList 的底层是一个数组.  ArrayList<String> list1 = new ArrayList<>(); list1.add("a ...

  5. 路由器wan口ip地址显示0.0.0.0怎么办

    http://m.xuexila.com/luyouqi/671049.html 这个网络时代里面我们最常用来连接网络的设备就是路由器了,现在的社会不管是工作还是生活几乎都离不开网络了,同时我们也要学 ...

  6. docker运行时设置redis密码并替换redis默认的dump.rdb

    docker run -itd --name test -p 6379:6379 -v /tmp/dump.rdb:/data/dump.rdb redis:4.0.8 --requirepass ' ...

  7. Sqlserver查询结果集插入新表

    数据库“Test” 数据库“Test2” 表 “fromTable” 表 “toTable” 表 “newTable” 字段 “name”,“age”,“gender” 原因:公司有2个数据库,一个是 ...

  8. Openjudge-4132-四则运算表达式求值

    这一题我们可以通过递归求解,首先我们可以把一个表达式分为三部分,分别是: (1)表达式 :项.加减 (2)项:因子.乘除 (3)因子:数.()表达式 这三项构成了递归的关系,我们可以看到,要求一个表达 ...

  9. html/css:简单网页

    html: <!DOCTYPE html> <html> <head> <meta http-equiv=“content-type” content=“te ...

  10. 如何用纯 CSS 创作一个容器厚条纹边框特效

    效果预览 在线演示 按下右侧的"点击预览"按钮在当前页面预览,点击链接全屏预览. https://codepen.io/zhang-ou/pen/YLqbXy 可交互视频教程 此视 ...