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 注意到边权为一 ...
随机推荐
- ELF文件解析(一):Segment和Section
ELF 是Executable and Linking Format的缩写,即可执行和可链接的格式,是Unix/Linux系统ABI (Application Binary Interface)规范的 ...
- 20155237 2016-2017-2 《Java程序设计》第8周学习总结
20155237 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 NIO与NIO2 认识NIO Channel: 衔接数据节点(与IO中的流对比) isOpe ...
- 线程异步操作,更新其中一个报错不影响另一个的运行(Task )
//子系统同步更新 BD001_BLL bll = new BD001_BLL(); List<BD001_Model> lis ...
- 【洛谷P2704【NOI2001】】炮兵阵地
题目描述 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用“H” 表示),也可能是平原(用“P”表示),如下图.在每一格平原地形上最 ...
- CF912E Prime Gift
传送门 看到\(n\)只有16,可以把这些质数分成两半,然后预处理出这些数相乘得出的小于\(10^{18}\)的所有数,排个序,然后二分最终答案,再用两个指针从前往后和从后往前扫,进行\(two-po ...
- WARN bzip2.Bzip2Factory: Failed to load/initialize native-bzip2 library system-native, will use pure-Java version
[root@hdp2 /root]#hadoop checknative -a 18/12/09 00:31:19 WARN bzip2.Bzip2Factory: Failed to load/in ...
- Python 入门基础18 --re模块+内存管理
今日内容: 1.垃圾回收机制 2.re模块 一.垃圾回收机制 在计算机中,不能被程序访问到的数,称之为垃圾 1.1 引用计数 引用计数用来记录值的内存地址被记录的次数 每引用一次就对标记 +1 操作 ...
- script标签中type为"text/x-template"或"text/html"
写过一点前端的都会碰到需要使用JS字符串拼接HTML元素然后append到页面DOM树上的情况,一般的写法都是使用+号以字符串的形式拼接,如果是短点的还好,如果很长很长的话就会拼接到令人崩溃了. 比如 ...
- linux笔记_day06
1.用户:表示符,凭证 2.用户组:表示符 进程也是有属主和属组的 安全上下文(secure context): 用户:UID,/etc/pawwd 组:GID ,/etc/group 影子口令: 用 ...
- WiFi基本知识【转】
转自:http://blog.csdn.net/myarrow/article/details/7930131 1. IE802.11简介 标准号 IEEE 802.11b IEEE 802.11a ...