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 3 1
2 4 4
6 6 6

样例输出

5 2
2 5
4 1
6 0

提示

易发现:三个点两两求出LCA,一定至少有两个LCA相等

    若只有两个相等,那聚集点就是剩下的LCA

    若三个相等,那聚集点就是LCA

 #include <algorithm>
#include <cstdio>
#include <vector> using namespace std; const int N(1e5*+);
vector<int>vec[N];
int n,m,x,y,z;
int anspoint,anssum;
int deep[N],dad[N],size[N],top[N]; void DFS(int x)
{
size[x]=; deep[x]=deep[dad[x]]+;
for(int i=;i<vec[x].size();i++)
if(dad[x]!=vec[x][i])
{
dad[vec[x][i]]=x;
DFS(vec[x][i]);
size[x]+=size[vec[x][i]];
}
} void DFS_(int x)
{
int t=; if(!top[x]) top[x]=x;
for(int i=;i<vec[x].size();i++)
if(vec[x][i]!=dad[x]&&size[t]<size[vec[x][i]]) t=vec[x][i];
if(t) top[t]=top[x],DFS_(t);
for(int i=;i<vec[x].size();i++)
if(vec[x][i]!=dad[x]&&t!=vec[x][i]) DFS_(vec[x][i]);
} int LCA(int x,int y)
{
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]]) swap(x,y);
x=dad[top[x]];
}
if(deep[x]>deep[y]) swap(x,y);
return x;
} int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
{
scanf("%d%d",&x,&y);
vec[x].push_back(y);
vec[y].push_back(x);
}
DFS(); DFS_();
for(;m;m--)
{
scanf("%d%d%d",&x,&y,&z);
anspoint=LCA(x,y)^LCA(x,z)^LCA(y,z);
anssum =deep[x]+deep[anspoint]-(deep[LCA(x,anspoint)]<<);
anssum+=deep[y]+deep[anspoint]-(deep[LCA(y,anspoint)]<<);
anssum+=deep[z]+deep[anspoint]-(deep[LCA(z,anspoint)]<<);
printf("%d %d\n",anspoint,anssum);
}
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 紧急集合

    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

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

  5. 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 ...

  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. poj1286 Necklace of Beads【裸polya】

    非常裸的polya,只是我看polya看了非常久 吉大ACM模板里面也有 #include <cstdio> #include <cmath> #include <ios ...

  2. 【为小白菜打call】

    作为本校的竞赛生,我必须为我大OJ打call caioj,小白菜oj,顾名思义,就是为刚踏进OI的“小白菜”们准备的网站,里面包含了许多专题内容,各种模版和讲解视频 而且对于刚学习C++的同学,更有帮 ...

  3. Ubuntu开机报错:could not update ICEauthority file /home/user/.ICEauthority(转载)

    解决方法如下: 一. 代码:sudo chown $USER:$USER /home/$USER/.ICEauthority        sudo chmod 644 /home/$USER/.IC ...

  4. 2017第33周四JDK8并发

    Java 8在Lambda表达式.接口默认方式.新的日期API等方面引入的新特性广受关注,同时在并发编程方面也做出了大量改进.以往的几个Java版本都对java.util.concurrent做了不同 ...

  5. Thread.setDaemon设置说明

    转载地址:http://blog.csdn.net/m13666368773/article/details/7245570 Thread.setDaemon的用法,经过学习以后了解: 1. setD ...

  6. kafka windows安装 命令行下使用测试

    1.zookeeper安装:   (https://zookeeper.apache.org/releases.html) ①进入zookeeper的相关设置所在的文件目录,例如本文的:D:\bigd ...

  7. Vmware VM共享

    打开虚拟机,设置,选项点击共享文件夹 点击启用,将电脑windows的目录添加过来 进入终端即可以进入

  8. Tomcat 初探(一) 简介

    简述 大部分入了 Java 开发这个坑的朋友,都需要把自己的网站发布到 Web 服务器上,相信也听过 Tomcat 的鼎鼎大名.Tomcat 是由 Sun 公司软件架构师詹姆斯·邓肯·戴维森开发的 W ...

  9. synchronized同步机制,修饰类和修饰对象的区别

    synchronized用法 synchronized修饰的对象有几种: 修饰一个类:其作用的范围是synchronized后面括号括起来的部分,作用的对象是这个类的所有对象: 修饰一个方法:被修饰的 ...

  10. PHP的反射API

    PHP5的类和对象并没有告诉我们类内的所有一切,而只是报告了他们的公共成员.要充分了解一个类,需要知道其私有 成员和保护成员,还要知道其方法所期望的参数,对此我们要使用API 1.获得反射API的转储 ...