当前位置:网站首页>LCA 三种姿势(倍增,Tarjan+并查集,树链剖分)
LCA 三种姿势(倍增,Tarjan+并查集,树链剖分)
2022-07-26 00:12:00 【lovesickman】
LCA 三种姿势(倍增,Tarjan+并查集,树链剖分)
1.倍增求 LCA O ( ( n + m ) l o g n ) O((n+m)logn) O((n+m)logn)
- d e p [ u ] dep[u] dep[u] 表示 u u u 节点的深度。
- f a [ u ] [ i ] fa[u][i] fa[u][i] 表示从 u u u 节点向上跳跃 2 i 2^i 2i 层的祖先节点。
Q1:为什么要定义成 2 i 2^i 2i , 3 i 3^i 3i 不是更快?
主要是因为需要用到二进制拆分,所有的数可以表示成某些 2 的次幂的和。
vector<int>e[N];
int dep[N],fa[N][20];
打表:倍增递推,从小到大枚举
void dfs(int u,int father){
dep[u] = dep[father] + 1;
fa[u][0] = father;
for(int i=1;i<=19;i++){
fa[u][i] = fa[fa[u][i-1]][i-1];
}
for(int v:e[u]){
if(v!=father)dfs(v,u);
}
}
int lca(int u,int v){
if(dep[u] < dep[v])swap(u,v);
// 2进制拆分,从大到下跳跃
for(int i=19;i>=0;i--){
// u不能跳到v上边去
if(dep[fa[u][i]]>=dep[v])
u = fa[u][i];
}
// v恰好是u的lca
if(u == v)return v;
// u,v一起跳
for(int i=19;i>=0;i--){
// 不能越界,一块跳到lca的下一个节点。
if(fa[u][i] != fa[v][i]){
u = fa[u][i];
v = fa[v][i];
}
}
return fa[u][0];
}
y总的板子:
void bfs(int root)
{
memset(depth, 0x3f, sizeof depth);
depth[0] = 0, depth[root] = 1;
int hh = 0, tt = 0;
q[0] = root;
while (hh <= tt)
{
int t = q[hh ++ ];
for (int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
if (depth[j] > depth[t] + 1)
{
depth[j] = depth[t] + 1;
q[ ++ tt] = j;
fa[j][0] = t;
for (int k = 1; k <= 15; k ++ )
fa[j][k] = fa[fa[j][k - 1]][k - 1];
}
}
}
}
int lca(int a, int b)
{
if (depth[a] < depth[b]) swap(a, b);
for (int k = 15; k >= 0; k -- )
if (depth[fa[a][k]] >= depth[b])
a = fa[a][k];
if (a == b) return a;
for (int k = 15; k >= 0; k -- )
if (fa[a][k] != fa[b][k])
{
a = fa[a][k];
b = fa[b][k];
}
return fa[a][0];
}
感觉 dfs 更短一些。
2.Tarjan离线求 LCA O ( ( n + m ) ) 线性! O((n+m))线性! O((n+m))线性!
- f a [ u ] fa[u] fa[u] 表示 u u u 节点的父亲节点。
- v i s [ u ] vis[u] vis[u] 标记数组,防止向回深搜
- 非常有特点的 vector<pair<int,int>>query[N] 表示 query[u]={v,i} 表示第 i i i 次询问 ( u , v ) (u,v) (u,v) 的 L C A LCA LCA 是什么。
- 每个查询必须正反建立一次!,不然结果不对
query[u] = {
v,i};
query[v] = {
u,i};
vector<int>e[N];
vector<pair<int,int>>query[N];
int fa[N],vis[N],ans[M];
int find(int u){
// 带路径压缩
return fa[u] == u ? fa[u] : fa[u] = find(fa[u]);
}
void tarjan(int u){
vis[u] = 1;
for(auto j:e[u]){
// O(n)
if(!vis[j]){
tarjan(j);
fa[j] = u; // 返回u时,j指向他的父亲u
}
}
// 离开u时,枚举以u为起点的LCA询问。 O(m)
for(auto q:query[u]){
int v = q.first;
int i = q.second;
if(vis[v]){
ans[i] = find(v);
}
}
}
3.树链剖分求 LCA O ( ( n + m ( 查询次数 ) l o g n ) ) O((n+m(查询次数)logn)) O((n+m(查询次数)logn))
前置知识:
重儿子 :父节点的所有儿子中子树节点最多的节点 (如果有多个重儿子,任选一个)。
轻儿子:父节点中除重儿子以外的儿子,(根也是轻儿子)。
重边:父节点和重儿子连成的边。
轻边:父节点和轻儿子连成的边。
重链: 由多条重边连接而成的路径,独立的节点自身也是重链。
- 整棵树会被剖分成若干条重链。
- 轻儿子一定是每条重链的顶点。
- 任意一条路径被切分成不超过 O ( l o g n ) O(logn ) O(logn) 条链。
f a [ u ] fa[u] fa[u] : u u u 的父节点。
d e p [ u ] dep[u] dep[u] : u u u 的深度
s o n [ u ] son[u] son[u] : u u u 的重儿子。
s z [ u ] sz[u] sz[u] : u u u 为根子树节点数。
t o p [ u ] top[u] top[u] : u u u 所在重链的顶点。
流程:
- 第一遍 dfs,预处理 f a [ ] , d e p [ ] , s o n [ ] , s z [ ] fa[\ ],dep[\ ],son[\ ],sz[\ ] fa[ ],dep[ ],son[ ],sz[ ]
- 第二遍 dfs,预处理 t o p [ ] top[\ ] top[ ]
- 做 L C A LCA LCA
轻儿子的链头一定是他自身,叶节点没有重儿子。
vector<int>e[N];
int fa[N],dep[N],son[N],sz[N];
int top[N];
void dfs1(int u,int father){
// O(n)
fa[u] = father,dep[u] = dep[father]+1,sz[u]=1;
for(int j:e[u]){
if(j==u)continue;
dfs1(j,u);
// 回溯之后更新子树节点数目和重儿子
sz[u] += sz[v];
if(sz[son[u]] < sz[v])son[u] = v;
}
}
void dfs2(int u,int t){
// 处理top数组,t记录重链头节点 O(n)
top[u] = t;
if(!son[u]) return ; // 遍历到了叶节点
dfs2(son[u],t); // 先欧重儿子
for(int j:e[u]){
if(j == fa[u] || j == son[u])continue;// 特判重复边和指向重儿子的边
dfs2(j,j); // 搜轻儿子
}
}
int lca(int u,int v){
// O(log n )
while(top[u]!=top[v]){
if(dep[top[u]] < dep[top[v]])swap(u,v);
u = fa[top[u]];
}
// u,v在同一条重链上,谁的深度浅返回谁。
return dep[u] < dep[v] ? u : v;
}
边栏推荐
- Pinduoduo gets the usage instructions of the product details API according to the ID
- Nodejs learning resources
- 12. Neural network model
- MWEC:一种基于多语义词向量的中文新词发现方法
- Jd.com searches for product API instructions by keyword
- 牛血清白蛋白修饰牛红细胞超氧化物歧化酶SOD/叶酸偶联2-ME白蛋白纳米粒的制备
- 合肥提前批
- [英雄星球七月集训LeetCode解题日报] 第25日 树状数组
- IP核:PLL
- OPENCV学习DAY6
猜你喜欢

IP核:PLL

Pikachu靶机通关和源码分析

Redirection and request forwarding

The way to understand JS: the principle of object.call and object.create() inheritance

对比7种分布式事务方案,还是偏爱阿里开源的Seata(原理+实战)

letfaw
34 use of sparksql custom functions, architecture and calculation process of sparkstreaming, dstream conversion operation, and processing of sparkstreaming docking Kafka and offset
![[redis] ① introduction and installation of redis](/img/87/af98c862524a81d4636f1cb3be5181.png)
[redis] ① introduction and installation of redis

牛血清蛋白修饰酚酸类及生物碱类小分子/偶联微球的蛋白/牛红细胞SOD的研究

J9 number theory: what is Dao mode? Obstacles to the development of Dao
随机推荐
Find and locate commands
@The difference between requestparam and @pathvariable annotations
Pinduoduo gets the usage instructions of the product details API according to the ID
redis的使用
What is Web3 game?
Opencv learning Day6
Trial division -- power of 3
URL address mapping configuration
Revision of Journal of Computational Physics
二进制表示--2的幂
Nest.js 用了 Express 但也没完全用
Sorting out the encapsulation classes of control elements in appium
"Demons dance", is the bull market over? 2021-05-13
MySQL - database log
MWEC:一种基于多语义词向量的中文新词发现方法
The way of understanding JS: what is prototype chain
The bull market is not over yet, and there is still 2021-05-18 in the second half
Understanding of "dbdnet: a deep boosting strategy for imagedenoising"
Use of redis
[contents] mqtt, nodejs projects

