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可能是深度最深的--但是我懒得 ...
随机推荐
- POJ 1658
#include<iostream>//cheng da cai zi using namespace std; int main() { int i; int time; ]; cin& ...
- sparksql链接mysql
1.在IDEA上建立一个sparksql_mysql的scala对象. 2.连接mysql的代码如下 import java.sql.{DriverManager, PreparedStatement ...
- C# TryXXXX模式
public static int? TrayParse(string text) { int ret; if (int.TryParse(text,out ret)) { return ret; } ...
- [主席树]HDOJ2665 && POJ2104 && POJ2761
主席树真是神奇的物种! Orz一篇资料 题意:给n.m 下面有n个数 (编号1到n) 有m个询问,询问的是上面的数的编号在[l,r]之间第k小的数 n.m的范围都是$10^5$ 是主席树的入门题 ...
- Android核心分析之二十三Andoird GDI之基本原理及其总体框架
Android GDI基本框架 在Android中所涉及的概念和代码最多,最繁杂的就是GDI相关的代码了.但是本质从抽象上来讲,这么多的代码和框架就干了一件事情:对显示缓冲区的操作和管理. GDI主 ...
- WordPress主题制作教程8:自定义page模版
创建新文件,头部注释 <?php /* Template Name: 新的page模版 */ ?> 在页面编辑右栏体现:
- 针对安卓java入门:类和对象
定义类 class Dog { String name; int age; void jump(){ } } 生成对象: public class Test { public static void ...
- [iOS问题归总]SourceTree+osChina版本管理出现的问题
1.commit失败(1) 解决办法: 把爆红的路径的文件删除掉, 重新commit 2. push时候, 有100MB限制 解决办法: Git只允许上传最大100MB的文件,如果超过,则会被serv ...
- Linux和远程系统同步文件(未完成)
实验环境: 本地主机:192.168.0.1 远程主机:192.168.0.101 1. 使用 scp,把/root/tardir1/achieve2.tar.gz复制到远程主机的root用户的hom ...
- sublime3配置Quick-X+自动错误提示
sublime3配置 安装Package Control 配置Quick-x API提示 配置Lua自动语法错误提示 sublime3 安装 Package Control View->Show ...