BZOJ1787 [Ahoi2008]Meet 紧急集合 LCA
欢迎访问~原文出处——博客园-zhouzhendong
去博客园看该题解
题目传送门 - BZOJ1787
题意概括
有一棵节点为n个(n≤500000)的树。接下来m次询问(m≤500000),每次给出3个点 a,b,c ,现在让你求一个点 p ,使得 dis(p,a) + dis(p,b) + dis(p,c) 最小。
输出 p 和 dis(p,a) + dis(p,b) + dis(p,c)。
题解
分别求3个LCA。
学习LCA -> 传送门
有两个一样的,那么另外一个就是答案。
代码
#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstdlib>
using namespace std;
const int N=500000+5,M=N*2;
struct Gragh{
int cnt,y[M],nxt[M],fst[N];
void set(){
cnt=0;
memset(fst,0,sizeof fst);
}
void add(int a,int b){
y[++cnt]=b,nxt[cnt]=fst[a],fst[a]=cnt;
}
}g;
int n,m,depth[N],anst[N][20];
void dfs(int prep,int rt){
depth[rt]=depth[anst[rt][0]=prep]+1;
for (int i=1;i<20;i++)
anst[rt][i]=anst[anst[rt][i-1]][i-1];
for (int i=g.fst[rt];i;i=g.nxt[i])
if (g.y[i]!=prep)
dfs(rt,g.y[i]);
}
int LCA(int a,int b){
if (depth[a]>depth[b])
swap(a,b);
for (int j=depth[b]-depth[a],i=0;j>0;j>>=1,i++)
if (j&1)
b=anst[b][i];
if (a==b)
return a;
for (int i=19;i>=0;i--)
if (anst[a][i]!=anst[b][i])
a=anst[a][i],b=anst[b][i];
return anst[a][0];
}
int main(){
scanf("%d%d",&n,&m);
g.set();
for (int i=1,a,b;i<n;i++){
scanf("%d%d",&a,&b);
g.add(a,b);
g.add(b,a);
}
depth[0]=-1;
memset(anst,0,sizeof anst);
dfs(0,1);
for (int i=1,a,b,c,ans,pos;i<=m;i++){
scanf("%d%d%d",&a,&b,&c);
int p1=LCA(a,b),p2=LCA(a,c),p3=LCA(b,c);
if (p1==p2)
pos=p3;
else if (p1==p3)
pos=p2;
else
pos=p1;
int q1=LCA(pos,a),q2=LCA(pos,b),q3=LCA(pos,c);
ans=depth[a]+depth[b]+depth[c]+depth[pos]*3-depth[q1]*2-depth[q2]*2-depth[q3]*2;
printf("%d %d\n",pos,ans);
}
return 0;
}
BZOJ1787 [Ahoi2008]Meet 紧急集合 LCA的更多相关文章
- 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 紧急集合(lca)
传送门 可以看出,三个点两两之间的lca会有一对相同,而另一个lca就是聚集点. 然后搞搞就可以求出距离了. ——代码 #include <cstdio> #include <cst ...
- BZOJ1787 [Ahoi2008]Meet 紧急集合[结论题]
location. 求到树上三点距离和最短的点及此距离. 这个不还是分类讨论题么,分两类大情况,如下图. 于是乎发现三个点对的lca中较深的那个lca是答案点.距离就是两两点对距离加起来除以2即可.这 ...
- LCA 【bzoj1787】[Ahoi2008]Meet 紧急集合
LCA [bzoj1787][Ahoi2008]Meet 紧急集合 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1787 注意到边权为一 ...
随机推荐
- Java图片比对
在自动化测试中,除了普通的值验证,经常还有一些图片验证,比如图片的匹配率,输出图片的差异图片等.本文主要用到了BufferedImage类来操作图片比对和输出差异图片,大体的思路如下: 1. 通过Im ...
- tensorflow实现mnist
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # 在变量的构建时,通过trunc ...
- Linux 查看文件编码
查看某个文件的编码格式:使用 vi 编辑器 打开文件: 按 Esc 输入 ” : set fileencoding “ 就会显示出来 文件的编码格式 : set fileencoding
- 拆分窗口QSplitter
拆分窗口中可以添加许多子控件,各个子控件通过拆分线相互分隔开来,拖动该拆分线可以随意改变子控件大小 import sys from PyQt5.QtCore import Qt from PyQt5. ...
- 一分钟了解contextlib模块
cobtextlib模块用于简化上下文管理器,其内置装饰漆@contextmanager,我们通过编写一个被contextmanager装饰的generator来简化上下文管理. from conte ...
- 通过Application传递数据
1:通过Application传递数据 假如有一个Activity A, 跳转到 Activity B ,并需要推荐一些数据,通常的作法是Intent.putExtra() 让Intent携带,或者有 ...
- winform程序生成条形码并且并且保存到本地文件中。
今天公司让做一个输入数字.字母生成条形码并且可以以图片格式保存到本地.当看到这个需求时候感觉很搞笑,明明可以用文本框搞定的东西非得做个程序.哎,寄人篱下,不多说了,这就是养兵千日用兵一时. 我在网上找 ...
- 之手算KD-tree
转自:https://zhuanlan.zhihu.com/p/27453420 本文来源于Machine Learning: Clustering & Retrieval | Courser ...
- numpy 中 shape_base提供的tile方法
tile函数 来自于numpy.lib.shape_base 功能:重复某个数组. 比如说tile(A, n), 功能是将数组A重复n次,构成一个新的数组(行数只有1个) 比如说tile(A, n, ...
- mysql授权报错 ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
授权用户时报错,ERROR 1819 (HY000): Your password does not satisfy the current policy requirements 原因为其实与val ...