【块状树】【LCA】bzoj1787 [Ahoi2008]Meet 紧急集合
分块LCA什么的,意外地快呢……
就是对询问的3个点两两求LCA,若其中两组LCA相等,则答案为第三者。
然后用深度减一减什么的就求出距离了。
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxn 500001
struct Graph
{int v[maxn<<],first[maxn<<],next[maxn<<],w[maxn<<],en;
void AddEdge(const int &a,const int &b)
{v[++en]=b;next[en]=first[a];first[a]=en;}}G;
int dep[maxn],fa[maxn],top[maxn],siz[maxn],x,y,z,sz,n,m;
int Abs(const int &x){return x< ? (-x) : x;}
int Res,Num;char C,CH[];
inline int R()
{
Res=;C='*';
while(C<''||C>'')C=getchar();
while(C>=''&&C<=''){Res=Res*+(C-'');C=getchar();}
return Res;
}
inline void P(long long x)
{
Num=;if(!x){putchar('');return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
}
void makeblock(int cur)
{
for(int i=G.first[cur];i;i=G.next[i])
if(G.v[i]!=fa[cur])
{
dep[G.v[i]]=dep[cur]+;
fa[G.v[i]]=cur;
if(siz[top[cur]]<sz)
{
siz[top[cur]]++;
top[G.v[i]]=top[cur];
}
makeblock(G.v[i]);
}
}
inline int QLCA(int u,int v)
{
while(u!=v)
{
if(top[u]==top[v])
{
if(dep[u]<dep[v]) swap(u,v);
u=fa[u];
}
else
{
if(dep[top[u]]<dep[top[v]]) swap(u,v);
u=fa[top[u]];
}
}
return u;
}
int main()
{
n=R();m=R();
for(int i=;i<n;i++)
{
x=R();y=R();
G.AddEdge(x,y);
G.AddEdge(y,x);
}
for(int i=;i<=n;i++) {top[i]=i; siz[i]=;}
sz=sqrt((double)n*5.0); makeblock();
for(;m>;m--)
{
x=R();y=R();z=R();
int f1=QLCA(x,y),f2=QLCA(x,z),f3=QLCA(y,z);
if(f1==f2)
{
int f4=QLCA(x,f3); P(f3); putchar(' ');
P(Abs(dep[f3]-dep[y])+Abs(dep[f3]-dep[z])
+Abs(dep[f4]-dep[x])+Abs(dep[f4]-dep[f3])); puts("");
}
else if(f1==f3)
{
int f4=QLCA(y,f2); P(f2); putchar(' ');
P(Abs(dep[f2]-dep[x])+Abs(dep[f2]-dep[z])
+Abs(dep[f4]-dep[y])+Abs(dep[f4]-dep[f2])); puts("");
}
else
{
int f4=QLCA(z,f1); P(f1); putchar(' ');
P(Abs(dep[f1]-dep[x])+Abs(dep[f1]-dep[y])
+Abs(dep[f4]-dep[z])+Abs(dep[f4]-dep[f1])); puts("");
}
}
return ;
}
【块状树】【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),每次 ...
- [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 ...
- LCA 【bzoj1787】[Ahoi2008]Meet 紧急集合
LCA [bzoj1787][Ahoi2008]Meet 紧急集合 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1787 注意到边权为一 ...
- BZOJ 1787: [Ahoi2008]Meet 紧急集合( 树链剖分 )
这道题用 LCA 就可以水过去 , 但是我太弱了 QAQ 倍增写LCA总是写残...于是就写了树链剖分... 其实也不难写 , 线段树也不用用到 , 自己YY一下然后搞一搞就过了...速度还挺快的好像 ...
随机推荐
- bzoj 1124 [POI2008]枪战Maf 贪心
[POI2008]枪战Maf Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 741 Solved: 295[Submit][Status][Disc ...
- Java Error: Failed to validate certificate. The application will not be executed
Hi, last week a customer had the problem that he wants to connect to the administration interface of ...
- abstract 与interface区别
1.abstract用于修饰类,interface用于修饰接口 2.抽象类中可以有抽象和非抽象方法,接口中只能定义抽象方法,不能有实现 3.抽象类必须被继承,interface被实现 4.抽象类有构造 ...
- 【bzoj1096-仓库建设】斜率优化
dsy1096: [ZJOI2007]仓库建设 [问题描述] L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚. 由于这座山处于高原内陆地区(干燥少雨),L公司一般把产品 ...
- 【bzoj3510】首都 LCT维护子树信息(+启发式合并)
题目描述 在X星球上有N个国家,每个国家占据着X星球的一座城市.由于国家之间是敌对关系,所以不同国家的两个城市是不会有公路相连的. X星球上战乱频发,如果A国打败了B国,那么B国将永远从这个星球消失, ...
- [bzoj2427][HAOI2010]软件安装——强连通分量+树形DP
题目大意 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和最大). 但是 ...
- vscode Python 运行环境配置
{ "git.ignoreMissingGitWarning": true, "window.zoomLevel": 1, "[python]&quo ...
- 【Python实例二】BeautifulSoup爬虫简单实践
前言 前面安装了BeautifulSoup库,现在就来实现一下吧. 目录 一.Urllib库的使用 二.BeautifulSoup的使用 三. 一个示例 ----------------------- ...
- c++文件流写入到execl中
#include <iostream> #include <fstream> #include <string> using namespace std; int ...
- 字节、字、bit、byte的关系[转]
字 word 字节 byte 位 bit 字长是指字的长度 1字=2字节(1 word = 2 byte) 1字节=8位(1 byte = 8bit) 一个字的字长为16 一个字节的字长是8 bps ...