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 注意到边权为一 ...
随机推荐
- MSVCR120.dll丢失问题
一.问题:丢失MSVCR120.dll 二.解决方法 到官网下载vcredist_x86.exe安装即可 地址:https://www.microsoft.com/en-us/download/det ...
- aircrack-ng套件学习笔记
Aircrack-ng套件 1.airdecap-ng 该工具主要用于对加密无线数据报文的解码. 1.当无线网络启用了WEP或者WPA-PASK加密,可以使用wireshark过滤,过滤条件为:IEE ...
- $_SERVER 当前信息
连接:https://www.cnblogs.com/mafeng/p/5868117.html $_SERVER['HTTP_ACCEPT_LANGUAGE']//浏览器语言 $_SERVER['R ...
- IO流总结笔记三
字节流: 抽象基类:InputStream, OutputStream. 字节流可以操作任何数据.注意:字符流使用的数组是字符数组.Char [] chs 字节流使用的数组是字节数组.Byte [ ...
- jira7.3.6添加导出excel的按钮
在 jira的application-data 文件夹下 jira-config.properties 文件(如果没有就创建一个) 中添加以下内容:jira.export.excel.enabled= ...
- Android避免OOM(内存优化)
Android内存优化是性能优化很重要的一部分,而如何避免OOM又是内存优化的核心. Android内存管理机制 android官网有一篇文章 Android是如何管理应用的进程与内存分配 Andro ...
- TabCtrl使用
TabCtrl使用 0x1 新建子页面 插入三个对话框,ID分别为:IDD_PAGE_FILE.IDD_PAGE_NETWORK.IDD_PAGE_PROCESS 工具箱-[属性]-[Style]设置 ...
- Hacker学习发展流程图
题记:梅花香自苦寒来.转载请注明版权:http://a1pass.blog.163.com/ A1Pass 今天看一位网友的日志上面有一篇名为“学黑的目标”的日志,里面有一个略显粗 ...
- Python3学习笔记19-继承和多态
在OOP程序设计中,当我们定义一个class的时候,可以从某个现有的class继承, 新的class称为子类(Subclass),而被继承的class称为基类.父类或超类(Base class.Sup ...
- jenkins checkstyle:local variable hides a field
源代码: 1 2 3 4 5 6 7 8 //应用上下文 private static ApplicationContext applicationContext; public static voi ...