1787: [Ahoi2008]Meet 紧急集合

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

HINT

题解:

n-1条边,很明显这是一棵树

那么题目就是求3个点的LCA

我们将3个点x,y,z分别求出x,y的LCA,设为a,x,z的为b,y,z的为c

则a,b,c 3个点中必有两个相等,答案就是另外一个(自已画画图就知道了)

那么再求3个点到它的距离就OK了。

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int n,m,i,j,x,y,z,a,b,c,t,p[],r[],f[][];
int tot,head[],Next[],to[];
void add(int x,int y)
{
tot++;
to[tot]=y;
Next[tot]=head[x];
head[x]=tot;
}
void dfs(int x)
{
int i;
for(i=head[x];i!=-;i=Next[i])
if(p[to[i]]==)
{
p[to[i]]=;
r[to[i]]=r[x]+;
f[to[i]][]=x;
dfs(to[i]);
}
}
int LCA(int a,int b)
{
int i;
if(r[a]<r[b]) swap(a,b);
for(i=;i>=;i--)
if(f[a][i]&&r[f[a][i]]>=r[b]) a=f[a][i];
if(a==b) return a;
for(i=;i>=;i--)
if(f[a][i]&&f[b][i]&&f[a][i]!=f[b][i])
{
a=f[a][i];
b=f[b][i];
}
return f[a][];
}
int dis(int x,int y)
{
int a=LCA(x,y);
return r[x]+r[y]-*r[a];
}
int main()
{
scanf("%d%d",&n,&m);
for(i=;i<=n;i++)
head[i]=-;
for(i=;i<n;i++)
{
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
p[]=;
r[]=;
dfs();
for(j=;j<=;j++)
for(i=;i<=n;i++)
f[i][j]=f[f[i][j-]][j-];
for(i=;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
a=LCA(x,y);
b=LCA(x,z);
c=LCA(y,z);
if(a==b) t=c;else
if(b==c) t=a;else
t=b;
printf("%d %d\n",t,dis(x,t)+dis(y,t)+dis(z,t));
}
return ;
}

bzoj 1787: [Ahoi2008]Meet 紧急集合的更多相关文章

  1. bzoj 1787 [Ahoi2008]Meet 紧急集合(1832 [AHOI2008]聚会)

    1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 1841  Solved: 857[Submit][ ...

  2. BZOJ 1787: [Ahoi2008]Meet 紧急集合( 树链剖分 )

    这道题用 LCA 就可以水过去 , 但是我太弱了 QAQ 倍增写LCA总是写残...于是就写了树链剖分... 其实也不难写 , 线段树也不用用到 , 自己YY一下然后搞一搞就过了...速度还挺快的好像 ...

  3. BZOJ 1787: [Ahoi2008]Meet 紧急集合 LCA

    1787: [Ahoi2008]Meet 紧急集合 Description Input Output Sample Input 6 4 1 2 2 3 2 4 4 5 5 6 4 5 6 6 3 1 ...

  4. BZOJ 1787: [Ahoi2008]Meet 紧急集合(lca+贪心)

    [Ahoi2008]Meet 紧急集合 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 ...

  5. BZOJ——1787: [Ahoi2008]Meet 紧急集合

    http://www.lydsy.com/JudgeOnline/problem.php?id=1787 题目描述 输入 输出 样例输入 6 4 1 2 2 3 2 4 4 5 5 6 4 5 6 6 ...

  6. bzoj 1787: [Ahoi2008]Meet 紧急集合【树链剖分lca】

    对于三个点求最小路径长度和,答案肯定在某两个点的lca上,因为如果把集合点定在公共lca上,一定有两个点汇合后再一起上到lca,这样显然不如让剩下的那个点下来 这个lca可能是深度最深的--但是我懒得 ...

  7. 1787: [Ahoi2008]Meet 紧急集合

    1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 1482  Solved: 652[Submit][ ...

  8. bzoj 1787 && bzoj 1832: [Ahoi2008]Meet 紧急集合(倍增LCA)算法竞赛进阶指南

    题目描述 原题连接 Y岛风景美丽宜人,气候温和,物产丰富. Y岛上有N个城市(编号\(1,2,-,N\)),有\(N-1\)条城市间的道路连接着它们. 每一条道路都连接某两个城市. 幸运的是,小可可通 ...

  9. bzoj1787 [Ahoi2008]Meet 紧急集合

    1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec  Memory Limit: 162 MB Submit: 2272  Solved: 1029 [Submi ...

随机推荐

  1. textarea输入框实时统计输入字符数

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. hdu 1232 畅通工程(并查集算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 畅通工程 Time Limit: 4000/2000 MS (Java/Others)    M ...

  3. arduino 用电位器调节LED闪烁频率

    int dianwei; int led = 13; void setup() {  // put your setup code here, to run once:  Serial.begin(9 ...

  4. 【遍历集合】Java遍历List,Map,Vector,Set的几种方法

    关于list,map,set的区别参考http://www.cnblogs.com/qlqwjy/p/7406573.html 1.遍历list @Test public void testList( ...

  5. 使用makecontext实现用户线程【转】

    转自:http://blog.csdn.net/cyberlabs/article/details/6920138 使用makecontext实现用户线程 现代Unix系统都在ucontext.h中提 ...

  6. udpserver.pl 和 udpclient.pl

    udpserver.pl #!use/bin/perl -w use Socket; #导入Socket库 ,INADDR_ANY);#压入sockaddr_in模式,利用了全局当地压缩地点INADD ...

  7. 【bzoj3786】星系探索

    ETT模版题. 真正的Eular-Tour-Tree维护的是树的欧拉序. 由于各种原因,没人知道怎么维护欧拉序,所以我写的是个假的,维护dfs序的. 本质还是用Splay维护序列. 然后因为我常数太差 ...

  8. FISCO-BCOS平台共识

    FISCO-BCOS 应用于区块链的多节点并行拜占庭容错共识算法 看了下微众平台的wiki共识知识 学习下 ()内是自己的思考  参考: https://github.com/FISCO-BCOS/W ...

  9. caffe solver.prototxt 生成

    from caffe.proto import caffe_pb2 s = caffe_pb2.SolverParameter() path='/home/xxx/data/' solver_file ...

  10. 微信小程序滚动条返回顶部

    scroll-view(可滚动视图区域): 使用竖向滚动时,需要给<scroll-view/>一个固定高度,通过 WXSS 设置 height,将scroll-y属性设置为true,将en ...