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. springboot 多线程的使用

    int pageSize = 10000; int totalCount = doctorDAO.selectDatasByMapCount2(jsonArray, false, null); int ...

  2. JVM配置参数解析

    1.参数说明(部分,待完善) -Xms128M:JVM初始分配的堆内存 -Xmx256M:JVM最大允许分配的堆内存,按需分配 -XX:PermSize=64M: JVM初始分配的非堆内存 -XX:M ...

  3. 外连接的用法 -- 《SQL进阶教程》 jupyter note

    import pandas as pd import sqlite3 conn = sqlite3.connect('1-5.db') 用外连接进行行列转换1(行 -> 列): 制作交叉表 怎么 ...

  4. Git_mergetool_tutorial(转载)

    原文链接:https://gist.github.com/karenyyng/f19ff75c60f18b4b8149#table-of-content Table Of Content Skip t ...

  5. PTA(Advanced Level)1031.Hello World for U

    Given any string of N (≥5) characters, you are asked to form the characters into the shape of U. For ...

  6. [转帖]ORA-00600: internal error code, arguments: [4193]问题解决

    ORA-00600: internal error code, arguments: [4193]问题解决 https://www.cnblogs.com/linyfeng/p/7496736.htm ...

  7. Centos8 安装ifconfig(net-tools.x86_64)

    情况: yum search net-tools.x86_64 没有找到结果 解决: 1.yum search net-tools 2. yum install -y net-tools.x86_64 ...

  8. CSP 画图(201512-3)

    问题描述 用 ASCII 字符来画图是一件有趣的事情,并形成了一门被称为 ASCII Art 的艺术.例如,下图是用 ASCII 字符画出来的 CSPRO 字样. ..____.____..____. ...

  9. JAVA重载和数组

    Java 重载:相同的方法名,但参数个数或者类型不一样的情况下,自动执行不同的方法 数组:   int[] array=new int[5];  System.out.println(array); ...

  10. springboot拦截中自动注入的组件为null问题解决方法

    一.写SpringUtil类来获取Springh管理的类实例,判断是否注入成功,如果没有注入成功重新获取注入 package com.util; import org.springframework. ...