bzoj1787 紧急集合
题目

Input

Output

分析
看到这个题不难想到倍增LCA,然后我们考虑如何计算。我们分别求出3个点中任意两点的LCA,为了走的步数最少所以肯定是先有两个点相遇然后另一个点走的它们相遇的点,至于最后的距离自己画一画就知道啦。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
#define ri register int
const int LOG=;
int pr[][LOG+],dep[],cnt;
int head[],to[],nxt[];
inline int read(){
int x=,f=;char s=getchar();
while(s<''||s>''){if(s=='-')f=-;s=getchar();}
while(s>=''&&s<=''){x=(x<<)+(x<<)+(s-'');s=getchar();}
return x*f;
}
inline void add(int x,int y){
to[++cnt]=y;
nxt[cnt]=head[x];
head[x]=cnt;
to[++cnt]=x;
nxt[cnt]=head[y];
head[y]=cnt;
}
inline void dfs(int x,int fa){
pr[x][]=fa;
for(ri i=head[x];i;i=nxt[i])
if(to[i]!=fa){
dep[to[i]]=dep[x]+;
dfs(to[i],x);
}
}
inline int lca(int x,int y){
if(dep[y]>dep[x])swap(x,y);
int ans=;
for(ri i=LOG;i>=;--i)
if(dep[x]-(<<i)>=dep[y])
x=pr[x][i];
if(x==y)return x;
for(ri i=LOG;i>=;--i)
if(pr[x][i]!=pr[y][i]){
x=pr[x][i];
y=pr[y][i];
}
return pr[x][];
}
int main()
{ int n,m,x,y,z;
n=read(),m=read();
for(ri i=;i<n;++i){
x=read(),y=read();
add(x,y);
}
dep[]=;
dfs(,);
for(ri j=;j<=LOG;++j)
for(ri i=;i<=n;++i)
pr[i][j]=pr[pr[i][j-]][j-];
for(ri i=;i<=m;++i){
x=read(),y=read(),z=read();
int a1=lca(x,y),a2=lca(x,z),a3=lca(y,z),ans;
if(a1==a2)ans=a3;
else if(a1==a3)ans=a2;
else if(a2==a3)ans=a1;
printf("%d %d\n",ans,dep[x]+dep[y]+dep[z]-dep[a1]-dep[a2]-dep[a3]);
}
return ;
}
bzoj1787 紧急集合的更多相关文章
- 【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 ...
- LCA 【bzoj1787】[Ahoi2008]Meet 紧急集合
LCA [bzoj1787][Ahoi2008]Meet 紧急集合 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1787 注意到边权为一 ...
- bzoj1787[Ahoi2008]Meet 紧急集合&bzoj1832[AHOI2008]聚会
bzoj1787[Ahoi2008]Meet 紧急集合 bzoj1832[AHOI2008]聚会 题意: 给个树,每次给三个点,求与这三个点距离最小的点. 题解: 倍增求出两两之间的LCA后,比较容易 ...
- bzoj1787 [Ahoi2008]Meet 紧急集合
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MB Submit: 2272 Solved: 1029 [Submi ...
- 【BZOJ-1787&1832】Meet紧急集合&聚会 倍增LCA
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 2259 Solved: 1023[Submit] ...
- BZOJ1787 [Ahoi2008]Meet 紧急集合 LCA
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1787 题意概括 有一棵节点为n个(n≤500000)的树.接下来m次询问(m≤500000),每次 ...
- 【bzoj1787】[Ahoi2008]Meet 紧急集合
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 2466 Solved: 1117[Submit] ...
- BZOJ1787 [Ahoi2008]Meet 紧急集合 【LCA】
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MB Submit: 3578 Solved: 1635 [Submi ...
- 【bzoj1787】&【bzoj1832】[Ahoi2008]Meet 紧急集合 & 聚会
bzoj1787就是bzoj1832 bzoj1832 空间和时间少了一些... 求三个结点到一个结点距离之和最小的结点以及距离和 求出两两lca,其中有两个相同,答案则为另一个 感觉就是一大暴力.. ...
随机推荐
- 服务器端使用DeferredResult异步推送技术
注意1: org.springframework.web.context.request.async.DeferredResult; 是Spring4.0以后的版本才有的,注意Spring版本,然后添 ...
- 2018.7.19 AK22 续集
话说上次坏机检查没有找到问题,后来我们联合软件工程师一起分析原因 ------------------------ 在线调试: MCU在内部晶振模式下:初始化正常,功能函数正常,切换到外部晶振12M后 ...
- 【SQL查询】查询结果分组_Group
1. 概述 “Group By”从字面意义上理解就是根据“By”指定的规则对数据进行分组 示例 2. group by的简单操作 3. Group By中Select指定的字段限制 select指定的 ...
- 两个VLC实现播放串流测试
实现原理: 一个VLC打开视频文件发布串流(格式HTTP.RTP.RTSP等),另一个VLC打开串流播放 发布串流步骤: 1.菜单“媒体”->“流”,先添加视频文件.选择“串流”,如下图: 2. ...
- Sortable
d_(:з」∠)_ import React, {Component} from 'react'; import "./app.css"; import Sortable from ...
- HDU2065"红色病毒"问题【指数型母函数】
Problem Description 医学界发现的新病毒因其蔓延速度和Internet上传播的"红色病毒"不相上下,被称为"红色病毒",经研究发现,该病毒及其 ...
- 快速构建一个 Springboot
快速构建一个 Springboot 官网:http://projects.spring.io/spring-boot/ Spring Boot可以轻松创建可以“运行”的独立的,生产级的基于Spring ...
- CUDA Pro Tip: Optimized Filtering with Warp-Aggregated Atomics
In this post, I’ll introduce warp-aggregated atomics, a useful technique to improve performance when ...
- HTML5的local storage
function clickLogin(){ var userName = $("#user").val().trim(); // console.log(userName); / ...
- phonegap制作windows phone包
下载SDK win7及以下版本下载SDK http://www.microsoft.com/zh-cn/download/confirmation.aspx?id=27570 WIN8下载SDK ht ...