【BZOJ1787】[Ahoi2008]Meet 紧急集合 LCA
【BZOJ1787】[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

题解:自己画画图就能看出来,对于询问(a,b,c),我们两两求出LCA,然后一定是选择深度最大的LCA作为集合点。就没有然后了
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=500010;
int n,m,cnt;
int to[maxn<<1],next[maxn<<1],head[maxn],fa[maxn][22],dep[maxn];
void dfs(int x)
{
for(int i=head[x];i!=-1;i=next[i])
{
if(to[i]==fa[x][0]) continue;
fa[to[i]][0]=x,dep[to[i]]=dep[x]+1,dfs(to[i]);
}
}
void add(int a,int b)
{
to[cnt]=b,next[cnt]=head[a],head[a]=cnt++;
}
int lca(int a,int b)
{
if(dep[a]<dep[b]) swap(a,b);
int i;
for(i=19;i>=0;i--) if(dep[fa[a][i]]>=dep[b]) a=fa[a][i];
if(a==b) return a;
for(i=19;i>=0;i--) if(fa[a][i]!=fa[b][i]) a=fa[a][i],b=fa[b][i];
return fa[a][0];
}
int main()
{
scanf("%d%d",&n,&m);
int i,j,a,b,c,x,y,z;
memset(head,-1,sizeof(head));
for(i=1;i<n;i++)
{
scanf("%d%d",&a,&b);
add(a,b),add(b,a);
}
dep[1]=1,dfs(1);
for(j=1;(1<<j)<=n;j++) for(i=1;i<=n;i++) fa[i][j]=fa[fa[i][j-1]][j-1];
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
x=lca(a,b),y=lca(b,c),z=lca(a,c);
if(dep[x]<dep[y]) swap(x,y);
if(dep[x]<dep[z]) swap(x,z);
if(dep[y]<dep[z]) swap(y,z);
printf("%d %d\n",x,dep[a]+dep[b]+dep[c]-2*dep[z]-dep[x]);
}
return 0;
}
【BZOJ1787】[Ahoi2008]Meet 紧急集合 LCA的更多相关文章
- BZOJ1787 [Ahoi2008]Meet 紧急集合 LCA
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1787 题意概括 有一棵节点为n个(n≤500000)的树.接下来m次询问(m≤500000),每次 ...
- bzoj1787[Ahoi2008]Meet 紧急集合&bzoj1832[AHOI2008]聚会
bzoj1787[Ahoi2008]Meet 紧急集合 bzoj1832[AHOI2008]聚会 题意: 给个树,每次给三个点,求与这三个点距离最小的点. 题解: 倍增求出两两之间的LCA后,比较容易 ...
- BZOJ1787 [Ahoi2008]Meet 紧急集合 【LCA】
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MB Submit: 3578 Solved: 1635 [Submi ...
- bzoj1787 [Ahoi2008]Meet 紧急集合
1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec Memory Limit: 162 MB Submit: 2272 Solved: 1029 [Submi ...
- 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 ...
- 【块状树】【LCA】bzoj1787 [Ahoi2008]Meet 紧急集合
分块LCA什么的,意外地快呢…… 就是对询问的3个点两两求LCA,若其中两组LCA相等,则答案为第三者. 然后用深度减一减什么的就求出距离了. #include<cstdio> #incl ...
- [bzoj1787][Ahoi2008]Meet 紧急集合(lca)
传送门 可以看出,三个点两两之间的lca会有一对相同,而另一个lca就是聚集点. 然后搞搞就可以求出距离了. ——代码 #include <cstdio> #include <cst ...
- BZOJ1787 [Ahoi2008]Meet 紧急集合[结论题]
location. 求到树上三点距离和最短的点及此距离. 这个不还是分类讨论题么,分两类大情况,如下图. 于是乎发现三个点对的lca中较深的那个lca是答案点.距离就是两两点对距离加起来除以2即可.这 ...
- LCA 【bzoj1787】[Ahoi2008]Meet 紧急集合
LCA [bzoj1787][Ahoi2008]Meet 紧急集合 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1787 注意到边权为一 ...
随机推荐
- iOS边练边学--UITabBarController的简单使用
一.UITabBarController的使用步骤 初始化UITabBarController 设置UIWindow的rootViewController为UITabBarController 根据具 ...
- 网络配置vlan
1. # This file describes the network interfaces available on your system # and how to activate them. ...
- easyui中datagrid用法,加载table数据与标题
加载标题写法: 多行标题:columns: [[ columns: [[ { field: 'itemid', title: 'Item ID', rows ...
- AOP——引言
转自:http://wayfarer.cnblogs.com/articles/241012.html 1.引言 2.AOP技术基础3.Java平台AOP技术研究 4..Net平台AOP技术研究 软件 ...
- 你可能并不需要一个 CTO
转自:http://dbanotes.net/CTO 有朋友在微信里让我给推荐一个 CTO.说是一家公司在找人,据说「项目不错」,因为之前的业务不是很互联网,现在有一个新的项目要做,要做一个社会化电商 ...
- fprintf宏
最近在调试程序,使用printf函数和调试信息都不能在终端输出,所以使用比较笨的方法.将调试信息写到文件中,再查看文件.由于要多次使用fprintf函数,所以将其写成宏. 参考链接: http://w ...
- 在tomcat下context.xml中配置各种数据库连接池(JNDI)
1. 首先,需要为数据源配置一个JNDI资源.我们的数据源JNDI资源应该定义在context元素中.在tomcat6版本中,context元素已经从server.xml文件中独立出来了,放在一个 ...
- 搭建局域网SVN代码服务器
1.安装Subversion,安装好后,在控制台输入“svn help”,如果成功安装,则会有很多命令打印输出:2.svnadmin create F:\Java_workspace\Reposito ...
- R语言低级绘图函数-rect
rect 函数用来在一张图上添加矩形,只需要指定左下角和右上角的坐标的位置,就可以画出一个矩形 基本用法: plot(1:5, 1:5, xlim = c(0,6), ylim = c (0,6), ...
- 【Java面试题】27 多线程笔试面试概念问答
第一题:线程的基本概念.线程的基本状态及状态之间的关系? 线程,有时称为轻量级进程,是CPU使用的基本单元:它由线程ID.程序计数器.寄存器集合和堆栈组成.它与属于同一进程的其他线程共享其代码段.数据 ...