LCA 【bzoj1787】[Ahoi2008]Meet 紧急集合
LCA 【bzoj1787】[Ahoi2008]Meet 紧急集合
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1787
注意到边权为一,x和y之间的距离为dep[x]+dep[y]-2*dep[LCA(x,y)]。
直接枚举三中LCA的情况即可。
我莫名其妙求了9次LCA,GXZ巨佬求了四次,emmm……
code
#include<iostream>
#include<cstdio>
using namespace std;
const int wx=1000017;
inline int read(){
int sum=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){sum=(sum<<1)+(sum<<3)+ch-'0';ch=getchar();}
return sum*f;
}
struct e{
int nxt,to;
}edge[wx*2];
int n,m,num;
int head[wx],f[wx][23],dep[wx];
void add(int from,int to){
edge[++num].nxt=head[from];
edge[num].to=to;
head[from]=num;
}
void dfs(int u,int fa){
dep[u]=dep[fa]+1;
for(int i=head[u];i;i=edge[i].nxt){
int v=edge[i].to;
if(v==fa)continue;
f[v][0]=u;dfs(v,u);
}
}
void pre(){
for(int j=1;j<=21;j++){
for(int i=1;i<=n;i++){
f[i][j]=f[f[i][j-1]][j-1];
}
}
}
int LCA(int x,int y){
if(dep[x]<dep[y])swap(x,y);
for(int i=21;i>=0;i--){
if(dep[f[x][i]]>=dep[y])x=f[x][i];
}
if(x==y)return x;
for(int i=21;i>=0;i--){
if(f[x][i]!=f[y][i])x=f[x][i],y=f[y][i];
}
return f[x][0];
}
int find(int x,int y,int z){
int lca=LCA(x,y);
int lca2=LCA(lca,z);
return dep[x]+dep[y]-2*dep[lca]+dep[z]+dep[lca]-2*dep[lca2];
}
void work(int x,int y,int z){
int dis1=find(x,y,z);
int dis2=find(x,z,y);
int dis3=find(y,z,x);
int maxdis=min(dis1,min(dis2,dis3));
if(dis1==maxdis){
printf("%d %d\n",LCA(x,y),dis1);
}
else if(dis2==maxdis){
printf("%d %d\n",LCA(x,z),dis2);
}
else printf("%d %d\n",LCA(y,z),dis3);
}
int main(){
n=read();m=read();
for(int i=1;i<n;i++){
int x,y;x=read();y=read();
add(x,y);add(y,x);
}
dfs(1,0);pre();
for(int i=1;i<=m;i++){
int x,y,z;
x=read();y=read();z=read();
work(x,y,z);
}
return 0;
}
LCA 【bzoj1787】[Ahoi2008]Meet 紧急集合的更多相关文章
- bzoj1787[Ahoi2008]Meet 紧急集合&bzoj1832[AHOI2008]聚会
bzoj1787[Ahoi2008]Meet 紧急集合 bzoj1832[AHOI2008]聚会 题意: 给个树,每次给三个点,求与这三个点距离最小的点. 题解: 倍增求出两两之间的LCA后,比较容易 ...
- 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 ...
- 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 紧急集合[结论题]
location. 求到树上三点距离和最短的点及此距离. 这个不还是分类讨论题么,分两类大情况,如下图. 于是乎发现三个点对的lca中较深的那个lca是答案点.距离就是两两点对距离加起来除以2即可.这 ...
- 【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 紧急集合
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 2466 Solved: 1117[Submit] ...
随机推荐
- 2018.6.21 HOLTEK HT49R70A-1 Source Code analysis
Cange note: “Reading TMR1H will latch the contents of TMR1H and TMR1L counter to the destination”? F ...
- Convolutional Neural Networks for Visual Recognition 2
Linear Classification 在上一讲里,我们介绍了图像分类问题以及一个简单的分类模型K-NN模型,我们已经知道K-NN的模型有几个严重的缺陷,第一就是要保存训练集里的所有样本,这个比较 ...
- Maven发布项目丢失Mybatis Mapper包的映射问题
由于一些eclipse版本问题,mybatis的mapper包中的sql文件没有被打进包,需要在pom中加入: <build> <!--配置打包时不过滤非java文件开始 --> ...
- 优秀开源项目之一:视频监控系统iSpy
iSpy是一个开源的视频监控软件,目前已经支持中文.自己用了一下,感觉还是很好用的.翻译了一下它的介绍. iSpy将PC变成一个完整的安全和监控系统 iSpy使用您的摄像头和麦克风来检测和记录声音或运 ...
- properties使用
properties可以load store 注释可以采用 "#" 或者"!" 分隔采用"="或者":" 分行采用&qu ...
- 51nod 1250 排列与交换——dp
题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1250 仔细思考dp. 第一问,考虑已知 i-1 个数有多少种方案. ...
- Linux环境下,开启tomcat时报transport error 202: bind failed: 地址已在使用
转载自:http://blog.csdn.net/mooncom/article/details/61913813 问题描述:今天我在Linux环境下配置tomcat,在tomcat/conf下的se ...
- virtual judge(专题一 简单搜索 E)
Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...
- Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: Cannot open connection
Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceE ...
- /* font-awesome-4.7.0的应用*/
<!DOCTYPE html> /* font-awesome-4.7.0的应用*/ <html lang="en"> <head> <m ...