BZOJ——1787: [Ahoi2008]Meet 紧急集合
http://www.lydsy.com/JudgeOnline/problem.php?id=1787
题目描述

输入

输出

样例输入
1 2
2 3
2 4
4 5
5 6
4 5 6
6 3 1
2 4 4
6 6 6
样例输出
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 紧急集合的更多相关文章
- bzoj 1787 [Ahoi2008]Meet 紧急集合(1832 [AHOI2008]聚会)
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 1841 Solved: 857[Submit][ ...
- BZOJ 1787: [Ahoi2008]Meet 紧急集合( 树链剖分 )
这道题用 LCA 就可以水过去 , 但是我太弱了 QAQ 倍增写LCA总是写残...于是就写了树链剖分... 其实也不难写 , 线段树也不用用到 , 自己YY一下然后搞一搞就过了...速度还挺快的好像 ...
- 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 ...
- 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 ...
- 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 ...
- bzoj 1787: [Ahoi2008]Meet 紧急集合【树链剖分lca】
对于三个点求最小路径长度和,答案肯定在某两个点的lca上,因为如果把集合点定在公共lca上,一定有两个点汇合后再一起上到lca,这样显然不如让剩下的那个点下来 这个lca可能是深度最深的--但是我懒得 ...
- 1787: [Ahoi2008]Meet 紧急集合
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 1482 Solved: 652[Submit][ ...
- bzoj 1787 && bzoj 1832: [Ahoi2008]Meet 紧急集合(倍增LCA)算法竞赛进阶指南
题目描述 原题连接 Y岛风景美丽宜人,气候温和,物产丰富. Y岛上有N个城市(编号\(1,2,-,N\)),有\(N-1\)条城市间的道路连接着它们. 每一条道路都连接某两个城市. 幸运的是,小可可通 ...
- bzoj1787 [Ahoi2008]Meet 紧急集合
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MB Submit: 2272 Solved: 1029 [Submi ...
随机推荐
- bzoj 3236: [Ahoi2013]作业(缺线段树)
3236: [Ahoi2013]作业 Time Limit: 100 Sec Memory Limit: 512 MBSubmit: 1744 Solved: 702[Submit][Status ...
- [SQL]存储过程建表
create PROC [dbo].CreateUserTable ( @name NVARCHAR(60) ) AS DECLARE @a NVARCHAR(max) SET @a='create ...
- Ubuntu16.04添加源的地址
打开terminal,输入sudo gedit /etc/apt/sources.list,向该文件中添加源的地址即可,如,可添加如下地址 deb-src http://archive.ubuntu. ...
- 使用ClassLoader类装载器获取系统资源
使用ClassLoader类装载器获取系统资源 2010-05-11 16:19:39 分类: Java /* ClassLoader 有两种方法获得系统资源,一个种静态方法,一种是实例方法. 静态方 ...
- 读配置文件能够保持顺序的 Java Properties 类
序 前几天,公司项目中有一个需求是读取配置文件的.并且最好可以保证载入到内存中的顺序可以和配置文件里的顺序一致,可是.假设使用 jdk 中提供的 Properties 类的话,读取配置文件后.载入到内 ...
- Android系统编译【转】
本文转载自;http://blog.csdn.net/zirconsdu/article/details/8005415 Android编译系统分析 概要 由于android编译系统的复杂和使用了不熟 ...
- File and Folder Permissions
https://msdn.microsoft.com/en-us/library/bb727008.aspx On NTFS volumes, you can set security permiss ...
- Swift 字符串的分割
componentsSeparatedByString 就是根据 传递的 一个字符,进行字符串的一个分割 // 分割字符串 - > let sourceArray = source.compon ...
- 转:如何在Ubuntu 14.04中安装最新版Eclipse
想必很多开发人员都知道,Ubuntu 软件源中提供的并不是最新版本的 Eclipse,本教程就教大家如何在 Ubuntu 14.04 中快速安装 Eclipse 官方发布的最新版本. 到目前为止,Ec ...
- (转载)Android快速开发偷懒必备,一句话搞定所有ViewGroup的Adapter . 支持自定义ViewGroup
[置顶] [Android]快速开发偷懒必备,一句话搞定所有ViewGroup的Adapter . 支持自定义ViewGroup 标签: androidAdapter快速开发0耦合 2016-12-1 ...