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. 网络编程介绍,C/S 架构,网络通讯协议,osi七层

    网络编程: 什么是网络编程: 网络通常指的是计算机中的互联网,是由多台计算机通过网线或其他媒介相互链接组成的 编写基于网络的应用程序的过程序称之为网络编程 为什么要学习网络编程: 我们已经知道计算机, ...

  2. FTP简单搭建(二)

    六.配套设置 1.基于用户名的上传和下载 创建用户 useradd alex echo redhat |passwd --stdin alex 指定用户登录的路径 可不设置,不设置则为用户家目录 mk ...

  3. git与gitlab工具

    1.Git和SVN的对比 1)git是分布式的,svn是集中式的.(最核心) 2)git是每个历史版本都存储完整的文件,便于恢复,svn是存储差异文件,历史版本不可恢复.(核心) 3)git可离线完成 ...

  4. 自然语言处理NLP学习笔记三:使用Django做一个NLP的Web站点

    前言: 前面我们已经能初步实现一个中文自然处理语言的模型了,但交互界面是命令行的,不太友好. 如果想做一个类似http://xiaosi.trs.cn/demo/rs/demo的界面,那就还需要继续往 ...

  5. 【Linux开发】linux设备驱动归纳总结(五):2.操作硬件——IO内存

    linux设备驱动归纳总结(五):2.操作硬件--IO内存 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...

  6. [python] super() 用法

    问题的发现与提出 在Python类的方法(method)中,要调用父类的某个方法,在Python 2.2以前,通常的写法如下: class A: def __init__(self): print & ...

  7. [转帖]JVM总结--JVM体系结构

    JVM总结--JVM体系结构 https://blog.csdn.net/samjustin1/article/details/52215274 需要不断的学习才可以. 2016年08月15日 22: ...

  8. Java基础(七)

    字符串String类 字符串的两个问题 构造方法 字符串池 字符串的内容不可变 比较方法 练习:模拟登陆 练习:模拟登陆(限制重试次数) 替换方法(敏感词过滤) 如果希望将字符串当中指定的部分进行替换 ...

  9. Memcached安装 常用指令

    Memcached 源码安装 # 安装依赖yum install -y gcc gcc-c++ automake autoconf make cmake libevent-devel.x86_64# ...

  10. 使用Python基于OpenCV的图像油画特效

    算法步骤: 1.获取图像的灰度图片 2.设计一个小方框(4x4/8x8 /10x10等),统计每个小方框的像素值 3.将0-255的灰度值划分成几个等级,并把第二步处理的结果映射到所设置的各个等级中, ...