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可能是深度最深的--但是我懒得 ...
随机推荐
- cf div2 238 c
C. Unusual Product time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)
做这道题之前,建议先做POJ 1151 Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...
- Web App中的Flexbox应用
虽然语法可能比较混杂,但 Flexbox 还是名不虚传的.它创造的是可伸缩的.有弹性的.可改变视觉顺序的智能盒子.它提供了简单的CSS布局方案范例让容器总是处于垂直水平居中的位置.使用盒模型来工作是非 ...
- POJ 1491
#include<iostream> #include<cmath> #include<iomanip> #define MAXN 50 using namespa ...
- HDU 1421 搬寝室
搬寝室 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- 从xml文件中读取注释
void Main() { string dirp=@"E:\Cread\UP4201308.bak\UP4.BAK\ExportPath\ConfigFile\"; ...
- POJ 1947 Rebuilding Roads(树形DP)
题目链接 题意 : 给你一棵树,问你至少断掉几条边能够得到有p个点的子树. 思路 : dp[i][j]代表的是以i为根的子树有j个节点.dp[u][i] = dp[u][j]+dp[son][i-j] ...
- MongoDB (六) MongoDB 集合操作
一. MongoDB 创建集合 createCollection() 方法 MongoDB db.createCollection(name, options) 是用来创建集合. 语法: 基本的 cr ...
- android-exploitme(四):参数篡改
今天我们来测试请求中参数的篡改,这个在web安全测试中是常用的,拦截请求包,修改参数,提交 1. 首先我们需要启动模拟器,并使用本机的代理(加上参数-partition-size的目的是为了可以往a ...
- WordPress主题制作教程2:导航菜单制作
实现自定义菜单,需要用到的函数是wp_nav_menu(); 在主题目录下的functions.php的 <?php ….. ?> 之间,添加以下菜单注册代码,这样你就可以在主题文件中使用 ...