location。

求到树上三点距离和最短的点及此距离。


这个不还是分类讨论题么,分两类大情况,如下图。

于是乎发现三个点对的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 紧急集合[结论题]的更多相关文章

  1. bzoj1787[Ahoi2008]Meet 紧急集合&bzoj1832[AHOI2008]聚会

    bzoj1787[Ahoi2008]Meet 紧急集合 bzoj1832[AHOI2008]聚会 题意: 给个树,每次给三个点,求与这三个点距离最小的点. 题解: 倍增求出两两之间的LCA后,比较容易 ...

  2. bzoj1787 [Ahoi2008]Meet 紧急集合

    1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec  Memory Limit: 162 MB Submit: 2272  Solved: 1029 [Submi ...

  3. BZOJ1787 [Ahoi2008]Meet 紧急集合 【LCA】

    1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec  Memory Limit: 162 MB Submit: 3578  Solved: 1635 [Submi ...

  4. BZOJ1787 [Ahoi2008]Meet 紧急集合 LCA

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1787 题意概括 有一棵节点为n个(n≤500000)的树.接下来m次询问(m≤500000),每次 ...

  5. 【块状树】【LCA】bzoj1787 [Ahoi2008]Meet 紧急集合

    分块LCA什么的,意外地快呢…… 就是对询问的3个点两两求LCA,若其中两组LCA相等,则答案为第三者. 然后用深度减一减什么的就求出距离了. #include<cstdio> #incl ...

  6. [bzoj1787][Ahoi2008]Meet 紧急集合(lca)

    传送门 可以看出,三个点两两之间的lca会有一对相同,而另一个lca就是聚集点. 然后搞搞就可以求出距离了. ——代码 #include <cstdio> #include <cst ...

  7. 【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 ...

  8. LCA 【bzoj1787】[Ahoi2008]Meet 紧急集合

    LCA [bzoj1787][Ahoi2008]Meet 紧急集合 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1787 注意到边权为一 ...

  9. 【bzoj1787】[Ahoi2008]Meet 紧急集合

    1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 2466  Solved: 1117[Submit] ...

随机推荐

  1. MongoDB 走马观花(全面解读篇)(转载)

    MongoDB 走马观花(全面解读篇)(转载)   目录 一.简介 二.基本模型 BSON 数据类型 分布式ID 三.操作语法 四.索引 索引特性 索引分类 索引评估.调优 五.集群 分片机制 副本集 ...

  2. RazorSQL for Mac如何编辑数据?

    RazorSQL 是一个非开源的功能非常强大数据库查询工具.SQL的编辑.数据库管理工具.支持通过 JDBC 和 ODBC 连接超过 29 种的数据库.允许您从一个数据库工具查询,更新,导航和管理所有 ...

  3. 【AMAD】tenacity -- Python中一个专门用来retry的库

    动机 简介 用法 基本用法 何时停止 尝试间的等待 何时retry 其它 热度分析 源码分析 个人评分 动机 很多时候,我们都喜欢为代码加入retry功能.比如oauth验证,有时候网络不太灵,我们希 ...

  4. ORACLE 左连接 右连接 内连接 外连接 全连接 五中表连接方式

    1.左连接 :left join 2.右连接:right join 3.内连接:inner join 4.外连接:outer join 5.全连接:full join

  5. base64图片数据类型转numpy的ndarray矩阵类型数据

    1.两种方法如下链接 https://www.cnblogs.com/mtcnn/p/9411683.html 2.第一种方法: # coding: utf-8 # python base64 编解码 ...

  6. Analysis Methods in Neural Language Processing: A Survey

    本文对神经语言处理中的分析方法进行了综述,并根据研究的突出趋势对其进行了分类,指出了存在的局限性,指出了今后研究的方向.

  7. nginx反向代理集群配置

    #user nobody;worker_processes 1; #error_log logs/error.log;#error_log logs/error.log notice;#error_l ...

  8. 学python的第三天

    函数的作用 不知道大家是否注意到,在上面的代码中,我们做了3次求阶乘,这样的代码实际上就是重复代码.编程大师Martin Fowler先生曾经说过:“代码有很多种坏味道,重复是最坏的一种!”,要写出高 ...

  9. (5.11)mysql高可用系列——复制中常见的SQL与IO线程故障

    关键词:mysql复制故障处理 [1]手工处理的gtid_next(SQL线程报错) 例如:主键冲突,表.数据库不存在,row模式下的数据不存在等. [1.1]模拟故障:GTID模式下的重复创建用户 ...

  10. Ajax异步上传在SSM框架中的应用

    最近在做毕业设计,由于毕设中要实现图片上传和视频上传的功能.突然发现原来的Form表单中的file已经满足不了我了,于是在一番折腾之后终于让我找到了一个简便的上传方式.下面来和大家分享一下我的过程. ...