Luogu P3379 【模板】最近公共祖先(LCA)
预处理出从$x$节点向上跳2i个节点的序号$p[x][i]$及节点深度$dpth[x]$,
寻找$lca$时,从$Max$(可能的最大深度)到0枚举$i$,
首先把较深的一个节点向上跳至深度相同,
然后两个点同步动作,若$p[x][i]≠p[y][i]$则跳。
最终返回他们的父亲$p[x][0]$即为$lca$。
注意双向边要开2倍
#include<cstdio>
#include<iostream>
#include<cmath>
#define MogeKo qwq using namespace std;
const int maxn = *;
int head[maxn],to[maxn],nxt[maxn],dpth[maxn];
int n,m,s,u,v,cnt,p[maxn][]; void add(int x,int y) {
to[++cnt] = y;
nxt[cnt] = head[x];
head[x] = cnt;
} void dfs(int x,int fa) {
dpth[x] = dpth[fa]+;
p[x][] = fa;
for(int i = ; ( << i)<=dpth[x]; i++)
p[x][i] = p[p[x][i-]][i-];
for(int i = head[x]; i; i = nxt[i]) {
if(to[i] == fa)continue;
dfs(to[i],x);
}
} int lca(int a,int b) {
if(dpth[a] < dpth[b])
swap(a,b);
for(int i = log2(dpth[a]); i >= ; i--)
if(dpth[a]-(<<i) >= dpth[b])
a = p[a][i];
if(a == b)return a;
for(int i = log2(dpth[a]);i >= ;i--)
if(p[a][i] != p[b][i]){
a = p[a][i];
b = p[b][i];
}
return p[a][];
} int main() {
scanf("%d%d%d",&n,&m,&s);
for(int i = ;i <= n-;i++){
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
dfs(s,-);
for(int i = ;i <= m;i++){
scanf("%d%d",&u,&v);
int t = lca(u,v);
printf("%d\n",t);
}
return ;
}
Luogu P3379 【模板】最近公共祖先(LCA)的更多相关文章
- [模板] 最近公共祖先/lca
简介 最近公共祖先 \(lca(a,b)\) 指的是a到根的路径和b到n的路径的深度最大的公共点. 定理. 以 \(r\) 为根的树上的路径 \((a,b) = (r,a) + (r,b) - 2 * ...
- Luogu 2245 星际导航(最小生成树,最近公共祖先LCA,并查集)
Luogu 2245 星际导航(最小生成树,最近公共祖先LCA,并查集) Description sideman做好了回到Gliese 星球的硬件准备,但是sideman的导航系统还没有完全设计好.为 ...
- POJ 1470 Closest Common Ancestors(最近公共祖先 LCA)
POJ 1470 Closest Common Ancestors(最近公共祖先 LCA) Description Write a program that takes as input a root ...
- POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA)
POJ 1330 Nearest Common Ancestors / UVALive 2525 Nearest Common Ancestors (最近公共祖先LCA) Description A ...
- 【lhyaaa】最近公共祖先LCA——倍增!!!
高级的算法——倍增!!! 根据LCA的定义,我们可以知道假如有两个节点x和y,则LCA(x,y)是 x 到根的路 径与 y 到根的路径的交汇点,同时也是 x 和 y 之间所有路径中深度最小的节 点,所 ...
- 【洛谷 p3379】模板-最近公共祖先(图论--倍增算法求LCA)
题目:给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 解法:倍增. 1 #include<cstdio> 2 #include<cstdlib> 3 #include ...
- 最近公共祖先(LCA)模板
以下转自:https://www.cnblogs.com/JVxie/p/4854719.html 首先是最近公共祖先的概念(什么是最近公共祖先?): 在一棵没有环的树上,每个节点肯定有其父亲节点和祖 ...
- HDU 2586 How far away ?(LCA模板 近期公共祖先啊)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the vi ...
- luogu3379 【模板】最近公共祖先(LCA) 倍增法
题目大意:给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 整体步骤:1.使两个点深度相同:2.使两个点相同. 这两个步骤都可用倍增法进行优化.定义每个节点的Elder[i]为该节点的2^k( ...
- 最近公共祖先lca模板
void dfs(int x,int root){//预处理fa和dep数组 fa[x][0]=root; dep[x]=dep[root]+1; for(int i=1;(1<<i)&l ...
随机推荐
- font-face在ie无法识别问题
font-face在ie的时候,需要其他格式eot,但是按照网上的设置无法识别,需要把原来的fotmat设置成format('eot');
- 我写的Angular相关的文章
此文正在更新中... Angular6的变化 Angular7的变化 No value accessor for form control with path的解决方案
- C# 利用ReportViewer生成报表
本文主要是利用微软自带的控件ReportViewer进行报表设计的小例子,仅供学习分享使用,如有不足之处,还请指正. 涉及知识点: ReportViewer :位于Microsoft.Reportin ...
- IDEA基于Maven Struts2搭建配置及示例
1.web.xml加载struts框架即过滤器,要注意struts版本不同过滤器配置也不同. <!DOCTYPE web-app PUBLIC "-//Sun Microsystems ...
- Android 如何解决dialog弹出时无法捕捉Activity的back事件
Android 如何解决dialog弹出时无法捕捉Activity的back事件 在一些情况下,我们需要捕捉back键事件,然后在捕捉到的事件里写入我们需要进行的处理,通常可以采用下面三种办法捕捉到b ...
- postgre中类似oracle的sql%rowcount用法
get diagnostics cnt := row_count; 现在有两个表tab1和tab2,两个表的格式相同,tab1中有1000条数据,tab2中0条数据 创建测试功能函数 create o ...
- Lua保留指定小数位数
默认会四舍五入 比如:%0.2f 会四舍五入后,保留小数点后2位 print(string.format("%.1f",0.26)) ---会输出0.3,而不是0.2 Lua保留一 ...
- JavaScript -- 时光流逝(八):js中的事件Event的使用
JavaScript -- 知识点回顾篇(八):js中的事件Event的使用 事件通常与函数配合使用,这样就可以通过发生的事件来驱动函数执行. (1) onabort : onabort 事件会在图像 ...
- March 09th, 2018 Week 10th Friday
All good things must come to an end. 好景无常. Love is when the other person's happiness is more importa ...
- February 22nd, 2018 Week 8th Thursday
Confine yourself to the present. 着眼当下. The morning wind spreads its fresh smell, we should get up an ...