BZOJ1787 [Ahoi2008]Meet 紧急集合[结论题]
求到树上三点距离和最短的点及此距离。
这个不还是分类讨论题么,分两类大情况,如下图。
于是乎发现三个点对的lca中较深的那个lca是答案点。距离就是两两点对距离加起来除以2即可。这个通过画图真的很好理解。毫无技术含量。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define dbg(x) cerr << #x << " = " << x <<endl
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
template<typename T>inline T _min(T A,T B){return A<B?A:B;}
template<typename T>inline T _min(T A,T B,T C){return _min(A,_min(B,C));}
template<typename T>inline T _max(T A,T B){return A>B?A:B;}
template<typename T>inline char MIN(T&A,T B){return A>B?(A=B,):;}
template<typename T>inline char MAX(T&A,T B){return A<B?(A=B,):;}
template<typename T>inline void _swap(T&A,T&B){A^=B^=A^=B;}
template<typename T>inline T read(T&x){
x=;int f=;char c;while(!isdigit(c=getchar()))if(c=='-')f=;
while(isdigit(c))x=x*+(c&),c=getchar();return f?x=-x:x;
}
const int N=5e5+;
int n,q;
struct thxorz{
int to,nxt;
}G[N<<];
int Head[N],tot;
inline void Addedge(int x,int y){
G[++tot].to=y,G[tot].nxt=Head[x],Head[x]=tot;
G[++tot].to=x,G[tot].nxt=Head[y],Head[y]=tot;
}
int depth[N],son[N],siz[N],fa[N],topfa[N];
#define y G[j].to
void dfs1(int x,int fat){
int res=-;fa[x]=fat;siz[x]=;
for(register int j=Head[x];j;j=G[j].nxt)if(y^fat){
depth[y]=depth[x]+;dfs1(y,x);siz[x]+=siz[y];
if(MAX(res,siz[y]))son[x]=y;
}
}
void dfs2(int x,int topf){
topfa[x]=topf;
if(!son[x])return;
dfs2(son[x],topf);
for(register int j=Head[x];j;j=G[j].nxt)if((y^fa[x])&&(y^son[x]))dfs2(y,y);
}
#undef y
inline int LCA(int x,int y){
while(topfa[x]^topfa[y]){
if(depth[topfa[x]]<depth[topfa[y]])_swap(x,y);
x=fa[topfa[x]];
}
return depth[y]<depth[x]?y:x;
}
inline void Query(int x,int y,int z){
int lca1=LCA(x,y),lca2=LCA(x,z),lca3=LCA(y,z);//dbg(lca1),dbg(lca2),dbg(lca3);
int minx=lca1;
if(depth[minx]<depth[lca2])minx=lca2;
if(depth[minx]<depth[lca3])minx=lca3;
int STOstostostoTHXorzorzorzORZ=(depth[x]+depth[y]-*depth[lca1]+depth[x]+depth[z]-*depth[lca2]+depth[z]+depth[y]-*depth[lca3])>>;
printf("%d %d\n",minx,STOstostostoTHXorzorzorzORZ);
}
int x,y,z;
int main(){//freopen("test.in","r",stdin);//freopen("test.out","w",stdout);
read(n);read(q);for(register int i=;i<n;++i)read(x),read(y),Addedge(x,y);
depth[]=,dfs1(,);dfs2(,);
while(q--)read(x),read(y),read(z),Query(x,y,z);
return ;
}
BZOJ1787 [Ahoi2008]Meet 紧急集合[结论题]的更多相关文章
- bzoj1787[Ahoi2008]Meet 紧急集合&bzoj1832[AHOI2008]聚会
bzoj1787[Ahoi2008]Meet 紧急集合 bzoj1832[AHOI2008]聚会 题意: 给个树,每次给三个点,求与这三个点距离最小的点. 题解: 倍增求出两两之间的LCA后,比较容易 ...
- bzoj1787 [Ahoi2008]Meet 紧急集合
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MB Submit: 2272 Solved: 1029 [Submi ...
- BZOJ1787 [Ahoi2008]Meet 紧急集合 【LCA】
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MB Submit: 3578 Solved: 1635 [Submi ...
- BZOJ1787 [Ahoi2008]Meet 紧急集合 LCA
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1787 题意概括 有一棵节点为n个(n≤500000)的树.接下来m次询问(m≤500000),每次 ...
- 【块状树】【LCA】bzoj1787 [Ahoi2008]Meet 紧急集合
分块LCA什么的,意外地快呢…… 就是对询问的3个点两两求LCA,若其中两组LCA相等,则答案为第三者. 然后用深度减一减什么的就求出距离了. #include<cstdio> #incl ...
- [bzoj1787][Ahoi2008]Meet 紧急集合(lca)
传送门 可以看出,三个点两两之间的lca会有一对相同,而另一个lca就是聚集点. 然后搞搞就可以求出距离了. ——代码 #include <cstdio> #include <cst ...
- 【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 ...
- LCA 【bzoj1787】[Ahoi2008]Meet 紧急集合
LCA [bzoj1787][Ahoi2008]Meet 紧急集合 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1787 注意到边权为一 ...
- 【bzoj1787】[Ahoi2008]Meet 紧急集合
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 2466 Solved: 1117[Submit] ...
随机推荐
- 使用shell脚本常见的一些问题
Jdk版本:jdk-8u102-linux-x64 Tomcat版本:apache-tomcat-7.0.92 Redis版本:redis-5.0.0 由于公司项目的需要,要在多台服务器上面部署一些应 ...
- SQL- 将一张表的数据插入到另一张表,表结构不一致(加条件)
公司业务需要,在对表进行操作的时候将操作人和操作记录记录到日志表里.记录下来以供参考和学习. 首先准备两张测试表:Info以及InfoLog 1.表结构相同的情况下: insert into Info ...
- PTA --- 天梯赛 L1-059 敲笨钟
L1-059 敲笨钟 (20 point(s)) 微博上有个自称“大笨钟V”的家伙,每天敲钟催促码农们爱惜身体早点睡觉.为了增加敲钟的趣味性,还会糟改几句古诗词.其糟改的方法为:去网上搜寻压“ong” ...
- Spring Cloud Bus 自动更新配置
---恢复内容开始--- Spring Cloud Config 结合 Spring Cloud bus 实现 git 仓库提交配置文件 触发消息队列 应用自动更新配置 1. config 服务端 添 ...
- Cisco的动态Nat、PAT
Lab_C(config)# interface Ethernet0/0 ip address 192.168.30.2 255.255.255.0 router rip network 172. ...
- (转) pip Fatal error in launcher: Unable to create process using
接上篇“Eclipse启动报错:JVM terminated. Exit code=2”,今天把Python的安装位置也从C盘剪切到了D盘, 然后修改了Path环境变量中对应的盘符:D:\Python ...
- hue改保存记录条数
参考: https://blog.csdn.net/liaoxiaoyi121121/article/details/80541901 需求: 开发需要保存查询记录的条数从10万改到100万 /etc ...
- python简单验证码识别
在学习python通过接口自动登录网站时,用户名密码.cookies.headers都好解决但是在碰到验证码这个时就有点棘手了:于是通过网上看贴,看官网完成了对简单验证码的识别,如果是复杂的请看大神的 ...
- linux 使用tmux
一. 什么是tmux 1.1. tmux 是两个单词的缩写,即“Terminal MultipleXer”,意思是“终端复用器“ 1.2. tmux 结构 1.2.1. tmux主要由三层: < ...
- 自动生成ID
public class IdUtil { /** * * @return 返回时间id,类似于20191217195622 */ public static String timeId(){ Dat ...