[bzoj1787][Ahoi2008]Meet 紧急集合(lca)
可以看出,三个点两两之间的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)的更多相关文章
- BZOJ1787 [Ahoi2008]Meet 紧急集合 LCA
		欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1787 题意概括 有一棵节点为n个(n≤500000)的树.接下来m次询问(m≤500000),每次 ... 
- bzoj1787[Ahoi2008]Meet 紧急集合&bzoj1832[AHOI2008]聚会
		bzoj1787[Ahoi2008]Meet 紧急集合 bzoj1832[AHOI2008]聚会 题意: 给个树,每次给三个点,求与这三个点距离最小的点. 题解: 倍增求出两两之间的LCA后,比较容易 ... 
- 【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 ... 
- BZOJ1787 [Ahoi2008]Meet 紧急集合  【LCA】
		1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MB Submit: 3578 Solved: 1635 [Submi ... 
- bzoj1787  [Ahoi2008]Meet 紧急集合
		1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MB Submit: 2272 Solved: 1029 [Submi ... 
- 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 ... 
- 【块状树】【LCA】bzoj1787 [Ahoi2008]Meet 紧急集合
		分块LCA什么的,意外地快呢…… 就是对询问的3个点两两求LCA,若其中两组LCA相等,则答案为第三者. 然后用深度减一减什么的就求出距离了. #include<cstdio> #incl ... 
- BZOJ1787 [Ahoi2008]Meet 紧急集合[结论题]
		location. 求到树上三点距离和最短的点及此距离. 这个不还是分类讨论题么,分两类大情况,如下图. 于是乎发现三个点对的lca中较深的那个lca是答案点.距离就是两两点对距离加起来除以2即可.这 ... 
- LCA 【bzoj1787】[Ahoi2008]Meet 紧急集合
		LCA [bzoj1787][Ahoi2008]Meet 紧急集合 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1787 注意到边权为一 ... 
随机推荐
- poj2441 Arrange the Bulls
			思路: 状态压缩dp.需要一点优化,否则容易超时. 实现: #include <cstdio> #include <vector> #include <cstring&g ... 
- java实现课堂随机点名小程序
			通过jdbc连接数据库实现读取学生花名册进行随机点名! ~jdbc连接mysql数据库 || 注释部分代码可通过读取.txt文档实现显示学生信息 ~通过点击开始按钮实现界面中间标签不断更新学生信息 ... 
- spark性能优化-JVM虚拟机垃圾回收调优
			1 2 3 4 
- linux下查看nginx配置文件地址
			which nginx/usr/sbin/nginx -t 
- IE8提速经验
			给人写了个web程序,其中detail页要加载不少东西,所以耗时略长.因为bootstrap的原因,我要求用户使用chrome; 而chrome出了名的快,所以也基本没觉得什么. 后来用户因为别的原因 ... 
- swfit:运算符重载  Operator Methods
			Operator Methods Classes and structures can provide their own implementations of existing operators. ... 
- gitlab利用ssh方式拉取代码
			问题1: Bad owner or permissions on .ssh/config的解决 当为本机配一个固定用户名远程登录某主机时,配置了一个config文件,但是在执行ssh免密码登录时报如下 ... 
- 阿里云人脸比对API封装
			这是根据封装是根据阿里云官方给的Demo进行修改的,当时是因为编写微信小程序云函数需要使用到阿里云人脸比对接口,才对其进行封装的. 记录下来,方便下次使用. 复制下来可以直接使用. 用到的依赖如下: ... 
- 数据库课程设计 PHP web实现
			纪念一下自己写的东西.. 都说很垃圾就是了 直接用XAMPP做的 菜鸟网上学的PHP和HTML <!DOCTYPE html> <html> <head> < ... 
- Linux常用命令大全3
			linux命令1,关机shutdown -h now2,init 0 关闭系统3,shutdown -h hours:minutes &按预定时间关闭系统4,shutdown -c取消按预定时 ... 
