这题有毒!!!!!!!!!!

TM我重新打的板子,然而。。。。。。

5分钟打完 debug两小时

我的写法常数太大了

每次DFS都要For去更新F

最后写了快读才A

改:

只处理f[i][0]

dfs结束在处理f

整整快了一倍多!!!!!!!!

靠!!

烦。。。。

#include<cstdio>
#include<iostream>
using namespace std;
#define olinr return
#define love_nmr 0
#define nmr 505050
struct node
{
int nxt,to;
}edge[nmr<<];
int head[nmr];
int n;
int m;
int root;
int f[nmr][];
int dep[nmr];
int cnt;
inline int read()
{
char ch=getchar();
int x=,f=;
while(!isdigit(ch))
{
if(ch=='-')
f=-f;
ch=getchar();
}
while(isdigit(ch))
{
x=(x<<)+(x<<)+(ch^);
ch=getchar();
}
return x*f;
}
inline void put(int x)
{
if(x<)
{
putchar('-');
x=-x;
}
if(x>)
put(x/);
putchar(x%+'');
}
inline void add(int from,int to)
{
cnt++;
edge[cnt].to=to;
edge[cnt].nxt=head[from];
head[from]=cnt;
}
inline void dfs(int x,int fa)
{
dep[x]=dep[fa]+;
f[x][]=fa;
for(int i=head[x];i;i=edge[i].nxt)
{
int go=edge[i].to;
if(go!=fa)
dfs(go,x);
}
}
inline void swap(int &x,int &y)
{
int t=x; x=y; y=t;
}
inline int LCA(int x,int y)
{
if(dep[x]<dep[y]) swap(x,y);
for(int i=;i>=;i--)
if(dep[x]-(<<i)>=dep[y])
x=f[x][i];
if(x==y) olinr x;
for(int i=;i>=;i--)
if(f[x][i]!=f[y][i])
{
x=f[x][i];
y=f[y][i];
}
olinr f[x][];
} int main()
{
n=read();
m=read();
root=read();
int x,y;
for(int i=;i<=n-;i++)
{
x=read();
y=read();
add(x,y);
add(y,x);
}
dfs(root,);
for(int j=;(<<j)<=n;j++)
for(int i=;i<=n;i++)
f[i][j]=f[f[i][j-]][j-];
for(int i=;i<=m;i++)
{
x=read();
y=read();
put(LCA(x,y));
putchar('\n');
}
olinr love_nmr;
}

P3379 【模板】最近公共祖先(LCA)(倍增)的更多相关文章

  1. [模板] 最近公共祖先/lca

    简介 最近公共祖先 \(lca(a,b)\) 指的是a到根的路径和b到n的路径的深度最大的公共点. 定理. 以 \(r\) 为根的树上的路径 \((a,b) = (r,a) + (r,b) - 2 * ...

  2. 【lhyaaa】最近公共祖先LCA——倍增!!!

    高级的算法——倍增!!! 根据LCA的定义,我们可以知道假如有两个节点x和y,则LCA(x,y)是 x 到根的路 径与 y 到根的路径的交汇点,同时也是 x 和 y 之间所有路径中深度最小的节 点,所 ...

  3. luogu3379 【模板】最近公共祖先(LCA) 倍增法

    题目大意:给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 整体步骤:1.使两个点深度相同:2.使两个点相同. 这两个步骤都可用倍增法进行优化.定义每个节点的Elder[i]为该节点的2^k( ...

  4. 最近公共祖先 LCA 倍增算法

          树上倍增求LCA LCA指的是最近公共祖先(Least Common Ancestors),如下图所示: 4和5的LCA就是2 那怎么求呢?最粗暴的方法就是先dfs一次,处理出每个点的深度 ...

  5. 最近公共祖先 LCA 倍增法

    [简介] 解决LCA问题的倍增法是一种基于倍增思想的在线算法. [原理] 原理和同样是使用倍增思想的RMQ-ST 算法类似,比较简单,想清楚后很容易实现. 对于每个节点u , ancestors[u] ...

  6. Luogu 2245 星际导航(最小生成树,最近公共祖先LCA,并查集)

    Luogu 2245 星际导航(最小生成树,最近公共祖先LCA,并查集) Description sideman做好了回到Gliese 星球的硬件准备,但是sideman的导航系统还没有完全设计好.为 ...

  7. POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)

    POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...

  8. POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)

    POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...

  9. 【洛谷 p3379】模板-最近公共祖先(图论--倍增算法求LCA)

    题目:给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 解法:倍增. 1 #include<cstdio> 2 #include<cstdlib> 3 #include ...

  10. 最近公共祖先(LCA)模板

    以下转自:https://www.cnblogs.com/JVxie/p/4854719.html 首先是最近公共祖先的概念(什么是最近公共祖先?): 在一棵没有环的树上,每个节点肯定有其父亲节点和祖 ...

随机推荐

  1. codeforces 622A A. Infinite Sequence (二分)

    A. Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. PS 滤镜— —Twirl Filter

    clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); I=imread ...

  3. bzoj 2794: Cloakroom dp

    题目: 有\(n\)件物品,每件物品有三个属性\(a_i,b_i,c_i,(a_i < b_i)\) 再给出\(q\)个询问,每个询问由非负整数\(m,k,s\)组成,问是否能够选出某些物品使得 ...

  4. MySQL 和 InnoDB

    发现一篇总结的很不错的文章,转一下 (原文作者:Draveness   原文链接:https://draveness.me/mysql-innodb) 作为一名开发人员,在日常的工作中会难以避免地接触 ...

  5. openfire服务器开发环境搭建

    2017-07-26 更新:直接获取最新版源码,就不会报错了,而且可支持的插件多,老版本的openfire几乎没有可用的官方插件. 系统及软件环境: MAC OSX EI Capitan 10.11. ...

  6. bzoj 2850 巧克力王国——KDtree

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2850 改一下估价即可.判断子树能否整个取或者是否整个不能取,时间好像就能行了? 因为有负数, ...

  7. numpy.zeros(shape, dtype=float, order='C')

    numpy.zeros Return a new array of given shape and type, filled with zeros. Parameters: shape : int o ...

  8. Select 标签的默认值设置

    以下是网上“借鉴”的:(http://blog.csdn.net/nairuohe/article/details/6307367/) 比如<select class="selecto ...

  9. nodejs中 underscore 包有什么作用

    Underscore一个JavaScript实用库,提供了一整套函数式变成有用的实用功能,但是没有扩展任何JavaScript内置对象.它是这个问题的答案:“如果我在一个空白的HTML页面前坐下, 并 ...

  10. CodeForces 492E Vanya and Field (思维题)

    E. Vanya and Field time limit per test 2 seconds memory limit per test 256 megabytes input standard ...