BZOJ 1787 紧急集合
LCA。注意细节。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxv 500500
#define maxe 1005000
using namespace std;
int n,m,x,y,z,g[maxv],nume=,anc[maxv][],dis[maxv];
struct edge
{
int v,nxt;
}e[maxe];
void addedge(int u,int v)
{
e[++nume].v=v;
e[nume].nxt=g[u];
g[u]=nume;
}
void dfs(int x)
{
for (int ee=;ee<=;ee++)
anc[x][ee]=anc[anc[x][ee-]][ee-];
for (int i=g[x];i;i=e[i].nxt)
{
int v=e[i].v;
if (v!=anc[x][])
{
anc[v][]=x;dis[v]=dis[x]+;
dfs(v);
}
}
}
int lca(int x,int y)
{
if (dis[x]<dis[y]) swap(x,y);
for (int e=;e>=;e--)
{
if ((dis[anc[x][e]]>=dis[y]) && (anc[x][e]!=))
x=anc[x][e];
}
if (x==y) return x;
for (int ee=;ee>=;ee--)
{
if (anc[x][ee]!=anc[y][ee])
{
x=anc[x][ee];
y=anc[y][ee];
}
}
return anc[x][];
}
int get_dis(int x,int y)
{
int k=lca(x,y);
return dis[x]+dis[y]-*dis[k];
}
int main()
{
scanf("%d%d",&n,&m);
for (int i=;i<=n-;i++)
{
scanf("%d%d",&x,&y);
addedge(x,y);
addedge(y,x);
}
dfs();
for (int i=;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
int t1,t2,t3;
t1=lca(x,y);t2=lca(x,z);t3=lca(y,z);
int ans1,ans2=;
if (t1==t2) ans1=t3;
else if (t1==t3) ans1=t2;
else ans1=t1;
ans2=get_dis(x,ans1)+get_dis(y,ans1)+get_dis(z,ans1);
printf("%d %d\n",ans1,ans2);
}
return ;
}
BZOJ 1787 紧急集合的更多相关文章
- BZOJ 1787 紧急集合(LCA)
转换成抽象模型,就是要求一棵树(N个点,有N-1条边表示这个图是棵树)中某一点满足给定三点a,b,c到某一点的距离和最小.那么我们想到最近公共祖先的定义,推出只有集合点在LCA(a,b).LCA(a, ...
- 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 Meet 紧急集合
Meet 紧急集合 这个题是在脖子oj(清北某奆佬给起的名字)八中oj(大视野在线评测)上的. 给出bzoj链接. 这个题还是求最近公共祖先的问题. 而该题不同于别的题,它是需要求三个点的最近公共祖先 ...
- 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 紧急集合
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 ...
- bzoj 1787: [Ahoi2008]Meet 紧急集合【树链剖分lca】
对于三个点求最小路径长度和,答案肯定在某两个点的lca上,因为如果把集合点定在公共lca上,一定有两个点汇合后再一起上到lca,这样显然不如让剩下的那个点下来 这个lca可能是深度最深的--但是我懒得 ...
随机推荐
- 在wpf窗体上添加用户控件
1.引用用户控件的命名控件 xmlns:my="clr-namespace:WpfApplicationDemo.Control" 2.把用户控件添加到窗体中 <my:Use ...
- editplus的各式插件
C/C++, Java, JSP, C#, .NET, SQL, Pascal, Python, Assembly, Basic files http://www.editplus.com/javac ...
- $headers = $this->input->request_headers();返回请求头(header)数组
请查看:CI中的输入类部分 建议用第一个 $headers = $this->input->request_headers() $this->input->request_he ...
- [C#] 區分 abstract、virtual、override 和 new
abstract.virtual.override和new是在類別的繼承關係中常用的四個修飾方法的關鍵字,在此略作總結. 1. 常用的中文名稱: n abstract => 抽象方法. n ...
- Linux软链接和硬链接
Linux中的链接有两种方式,软链接和硬链接.本文试图清晰彻底的解释Linux中软链接和硬链接文件的区别. 1.Linux链接文件 1)软链接文件 软链接又叫符号链接,这个文件包含了另一个文件的路径 ...
- android-exploitme(四):参数篡改
今天我们来测试请求中参数的篡改,这个在web安全测试中是常用的,拦截请求包,修改参数,提交 1. 首先我们需要启动模拟器,并使用本机的代理(加上参数-partition-size的目的是为了可以往a ...
- linux shell 命令学习(3) split - split a file into pieces
split 用来进行文件分割的指令 split [OPTION]... [INPUT [PREFIX]] 发现这个命令是因为有个需求,有个10W行的文本文件,需要分成5个2w行的文本文件, 查了一下资 ...
- C++:获取数组长度
C/C++中如何获取数组的长度? 如何获取数组的长度 2010-12-15 20:49 C/C++中如何获取数组的长度? 收藏 C.C++中没有提供 直接获取数组长度的函数,对于存放字符串 ...
- Git - Tutorial官方【转】
转自:http://www.vogella.com/tutorials/Git/article.html#git_rename_branch Lars Vogel Version 5.8 Copyri ...
- AE 栅格数据使用总结
RasterBand)的数据组成,一个波段就是一个数据矩阵.对于格网数据(DEM数据)和单波段的影像数据,表现为仅仅只有一个波段数据的栅格数据集,而对于多光谱影像数据则表现为具有多个波段的栅格数据集. ...