1787: [Ahoi2008]Meet 紧急集合

Time Limit: 20 Sec  Memory Limit: 162 MB
Submit: 2466  Solved: 1117
[Submit][Status][Discuss]

Description

Input

Output

Sample Input

6 4
1 2
2 3
2 4
4 5
5 6
4 5 6
6 3 1
2 4 4
6 6 6

Sample Output

5 2
2 5
4 1
6 0
 
 
【题解】
记住一个结论:三个点的最小距离点等于两两的lca中与其他两个lca不同的lca
然后就很水了。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<algorithm>
using namespace std;
#define MAXN 500010
struct node{int y,next;}e[MAXN*];
int n,m,len,ans,Link[MAXN],f[MAXN],deep[MAXN],anc[MAXN][];
inline int read()
{
int x=,f=; char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-; ch=getchar();}
while(isdigit(ch)) {x=x*+ch-''; ch=getchar();}
return x*f;
}
void insert(int x,int y) {e[++len].next=Link[x];Link[x]=len;e[len].y=y;}
void dfs(int x,int fa)
{
anc[x][]=f[x];
for(int i=;i<=;i++) anc[x][i]=anc[anc[x][i-]][i-];
for(int i=Link[x];i;i=e[i].next)
if(e[i].y!=fa)
{
deep[e[i].y]=deep[x]+;
f[e[i].y]=x;
dfs(e[i].y,x);
}
}
int lca(int x,int y)
{
if(deep[x]<deep[y]) swap(x,y);
for(int i=;i>=;i--) if(deep[anc[x][i]]>=deep[y]) x=anc[x][i];
if(x==y) return x;
for(int i=;i>=;i--) if(anc[x][i]!=anc[y][i]) x=anc[x][i],y=anc[y][i];
return f[x];
}
int cal(int a,int b,int c) {if(a==b)return c;else if(a==c)return b;else return a;}
int dis(int x,int y) {int t=lca(x,y);return deep[x]+deep[y]-*deep[t];}
int main()
{
//freopen("cin.in","r",stdin);
//freopen("cout.out","w",stdout);
n=read(); m=read();
for(int i=;i<n;i++) {int x=read(),y=read(); insert(x,y); insert(y,x);}
deep[]=; dfs(,);
for(int i=;i<=m;i++)
{
int x=read(),y=read(),z=read();
int a=lca(x,y),b=lca(x,z),c=lca(y,z);
int p=cal(a,b,c);
ans=dis(x,p)+dis(y,p)+dis(z,p);
printf("%d %d\n",p,ans);
}
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 紧急集合[结论题]

    location. 求到树上三点距离和最短的点及此距离. 这个不还是分类讨论题么,分两类大情况,如下图. 于是乎发现三个点对的lca中较深的那个lca是答案点.距离就是两两点对距离加起来除以2即可.这 ...

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

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

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

随机推荐

  1. HTTP 方法:GET与 POST

    什么是 HTTP? 超文本传输协议(HTTP)的设计目的是保证客户机与服务器之间的通信. HTTP 的工作方式是客户机与服务器之间的请求-应答协议. web 浏览器可能是客户端,而计算机上的网络应用程 ...

  2. 深入理解java虚拟机-第12章Java内存模型与线程

    第12章 Java内存模型与线程 Java内存模型  主内存与工作内存: java内存模型规定了所有的变量都在主内存中,每条线程还有自己的工作内存. 工作内存中保存了该线程使用的主内存副本拷贝,线程对 ...

  3. HDU - 6242:Geometry Problem(随机+几何)

    Alice is interesting in computation geometry problem recently. She found a interesting problem and s ...

  4. C++11新特性之右值引用(&&)、移动语义(move)、完美转换(forward)

    1. 右值引用 个人认为右值引用的目的主要是为了是减少内存拷贝,优化性能. 比如下面的代码: String Fun() { String str = "hello world"; ...

  5. JAVA如何以追加的方式向文件中写入信息?

    以FileWriter类为例: FileWriter的构造方法中有一个方法是:FileWriter(String fileName, boolean append)  ,其中第二个参数决定了写文件的方 ...

  6. NOIP2013DAY1题解

    T1转圈游戏 十月のsecret 题解:快速幂 代码: #include<iostream> #include<cstring> #include<cstdio> ...

  7. ArcGIS_Desktop安装步骤

      1.双击镜像文件 2.下一步 3.关闭 4.下一步 5.下一步 6.下一步 7.下一步 8.下一步 9.安装 10.一直到下一步安装完成

  8. UOJ #188 Sanrd —— min_25筛

    题目:http://uoj.ac/problem/188 参考博客:https://www.cnblogs.com/cjoieryl/p/10149748.html 关键是枚举最小质因子...所以构造 ...

  9. laravel 中config的使用

    在laravel的config中添加配置文件(比如:alipay.php)文件内容为return数组的形式 在方法中使用config()函数获取数据 $config = config('alipay. ...

  10. python基础篇之进阶

    python基础篇之进阶 参考博客:http://www.cnblogs.com/wupeiqi/articles/5115190.html python种类 1. cpython  使用c解释器生产 ...