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] ...
随机推荐
- pubwin2009 备份文件恢复
local_db.DBBak 恢复注意: 1.先安装好全新server -> 用SQLManager连接数据库 连接时右键编辑 SQLSERVER注册属性 默认sql连接 帐号netcafe ...
- 通道的分离与合并,ROI,
通道的分离与合并 class Program { static void Main(String[] args) { Mat img = CvInvoke.Imread(@"C:\Users ...
- 西安邀请赛-E(树链剖分+线段树)
题目链接:https://nanti.jisuanke.com/t/39272 题意:给一棵树,n个结点,树根为1,n-1条边,每个结点有一个权值.进行3种操作: 1 s t:把1和s之间的最短路径上 ...
- [转帖].MegaRAID SAS 9361-8i 开箱 极简测试
[配件开箱] 谁说raid0软硬没多大区别的...MegaRAID SAS 9361-8i 开箱 极简测试 [复制链接] https://www.chiphell.com/thread-1810952 ...
- # Excel批量处理数据
Excel批量处理数据 拖住框下拉即可得到每行+3的结果
- python多进程,并获取每个进程的返回值
pool = multiprocessing.Pool(processes=10) row = [...] for row in rows: task_id = row[1] img_id = row ...
- python基础之函数当中的装饰器
在实际工作当中存在一个开放封闭原则 1.对扩展是开放的 为什么要对扩展开放呢? 我们说,任何一个程序,不可能在设计之初就已经想好了所有的功能并且未来不做任何更新和修改.所以我们必须允许代码扩展.添加新 ...
- 基础数据类型之集合和深浅copy,还有一些数据类型补充
集合 集合是无序的,不重复的数据集合,它里面的元素是可哈希的(不可变类型),但是集合本身是不可哈希(所以集合做不了字典的键)的.以下是集合最重要的两点: 去重,把一个列表变成集合,就自动去重了. 关系 ...
- 日常用User-Agent列表
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET C ...
- 丑陋的 shell 来一遍
学习使人进步,好记性不如烂笔头 在线正则 在线正则 ^(.)(.).*?\2\1$ 正式开始总结 cat 反过来读取文件 tac file.txt nl --> number line 如 ca ...