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

Input

Output

Sample Input
1 2
2 3
2 4
4 5
5 6
4 5 6
6 3 1
2 4 4
6 6 6
Sample Output
2 5
4 1
6 0
HINT
题解:求出两两lca,其中有两个相同,答案则为另一个,参考HZWer.
/// #include<bits/stdc++.h>
using namespace std;
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){
if(ch=='-')f=-;ch=getchar();
}
while(ch>=''&&ch<=''){
x=x*+ch-'';ch=getchar();
}return x*f;
}
//****************************************
const int N=;
#define mod 1000000007
#define inf 1000000007
int n,m,head[N],t,vis[N],deep[N],fa[N][];
struct ss {
int to,next;
}e[N*];
void add(int u,int v) {
e[t].next=head[u];e[t].to=v;head[u]=t++;
}
void init() {
t=;mem(head);mem(vis);mem(fa);mem(deep);
}
void dfs(int x) {
vis[x]=;
for (int i=; i<= ;i++) {
if(deep[x]<(<<i)) break;
fa[x][i] = fa[fa[x][i-]][i-];
}
for (int i=head[x];i;i=e[i].next) {
if(vis[e[i].to]) continue;
deep[e[i].to]=deep[x]+;
fa[e[i].to][]=x;
dfs(e[i].to);
}
}
int RMQ_LCA(int x,int y) {
if(deep[x]<deep[y]) swap(x,y);
int d=deep[x]-deep[y];
for (int i=; i<= ;i++)
if((<<i)&d) x=fa[x][i];
for (int i=; i>= ;i--) {
if(fa[x][i]!=fa[y][i]) {
x=fa[x][i];y=fa[y][i];
}
}
if(x==y) return x;
else return fa[x][];
}
int Dis_LCA(int x,int y) {
int LCA= RMQ_LCA(x,y);
return (deep[x]+deep[y]-*deep[LCA]);
}
int cal(int x,int y,int z) {
int A;
int L1=RMQ_LCA(x,y);
int L2=RMQ_LCA(y,z);
int L3=RMQ_LCA(x,z);
if(L1==L2) {
A=L3;
}
else if(L2==L3) {
A=L1;
}
else A=L2;
int anss=Dis_LCA(x,A)+Dis_LCA(y,A)+Dis_LCA(z,A);
printf("%d %d\n",A,anss);
//cout<<A<<" "<<anss<<endl;
}
int main() {
scanf("%d%d",&n,&m);
init();
for(int i=;i<n;i++) {
int u,v;
scanf("%d%d",&u,&v);
add(u,v);add(v,u);
}
dfs();
for(int i=;i<=m;i++) {
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
cal(a,b,c);
} return ;
}
代码
BZOJ 1787: [Ahoi2008]Meet 紧急集合 LCA的更多相关文章
- 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+贪心)
[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可能是深度最深的--但是我懒得 ...
- 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 ...
- 1787: [Ahoi2008]Meet 紧急集合
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 1482 Solved: 652[Submit][ ...
- 【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 ...
- bzoj 1787 && bzoj 1832: [Ahoi2008]Meet 紧急集合(倍增LCA)算法竞赛进阶指南
题目描述 原题连接 Y岛风景美丽宜人,气候温和,物产丰富. Y岛上有N个城市(编号\(1,2,-,N\)),有\(N-1\)条城市间的道路连接着它们. 每一条道路都连接某两个城市. 幸运的是,小可可通 ...
随机推荐
- 给定的逗号分隔的数字字符串转换为Table
--将给定的逗号分隔的数字字符串转换为Table CREATE FUNCTION [dbo].[fu_Split](@strString nvarchar(4000)) RETURNS @Result ...
- 实现X*N
#include<iostream> using namespace std; double foo(int n,double x) { if(1==n) { return x; } el ...
- 转 IDEA 解决代码提示功能消失
转载路径是 https://blog.csdn.net/hmily_hui/article/details/78213037 原文地址:https://github.com/Damao/Intell ...
- O-理解共享池
我们可以通过show sga命令查看共享池的整体组成部分: ....待截图.... 一.SGA内存结构 Oracle中SGA主要包括: 1.固定数据结构部分(FIXED Size) 2.数据块缓冲区( ...
- Centos6.6 安装基于系统认证的vsftp服务
一.介绍 vsftp是一款文件服务器软件,在文件共享,代码更新,文件备份中也是经常用到,以下是基本安装环境: 1)CentOS6.6 2)vsftpd-2.2.2 二.安装 $ yum install ...
- Discuz 首页图文列表实现
<div id="shoucang"> <!--{eval $list_count=0;}--> <!--{loop $threadlist $thr ...
- Makefile精髓篇【转】
什么是makefile?或许非常多Winodws的程序猿都不知道这个东西,由于那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和professional的程序猿,makefile还是 ...
- CentOS安装Nodejs-v8.11.1
(1)到NodeJs官网(https://nodejs.org/en/download/),复制下载链接 (2)下载并解压 命令:wget https://nodejs.org/dist/v8.11. ...
- cshtml中字符串中表示特殊字符@
用“@@”表示字符串中的特殊字符@
- Ansible 利用playbook批量部署Nginx
我这里直接部署的,环境已经搭建好,如果不知道的小伙伴可以看上一遍ansible搭建,都写好了,这里是根据前面环境部署的 192.168.30.21 ansible 192.168.30.25 ...