当前位置:网站首页>LCA 板子
LCA 板子
2022-07-29 04:03:00 【Snow_raw】
LCA 板子
变量:
i n t int int f a [ N ] [ 25 ] ( 第二维取决于 N 大小,跳表 ) fa[N][25](第二维取决于N大小,跳表) fa[N][25](第二维取决于N大小,跳表)
i n t int int d e p [ N ] ( 点深度 ) dep[N](点深度) dep[N](点深度)
i n t int int h [ N ] , e [ M ] , n e [ M ] , i d x ( 邻接表 ) h[N],e[M],ne[M],idx(邻接表) h[N],e[M],ne[M],idx(邻接表)
求 求 求 d f s dfs dfs 序变量: 序变量: 序变量:
i n t int int t i m e s t a m p timestamp timestamp
i n t int int d f n [ N ] dfn[N] dfn[N]
关键函数:
void add(int a,int b){
e[idx]=b;
ne[idx]=h[a];
h[a]=idx++;
}
void bfs(int root){
memset(dep,0x3f,sizeof dep);
dep[root]=1;
dep[0]=0;//哨兵
queue<int>q;
q.push(root);
while(q.size()){
auto t=q.front();
q.pop();
for(int i=h[t];i!=-1;i=ne[i]){
int j=e[i];
if(dep[j]>dep[t]+1){
dep[j]=dep[t]+1;
q.push(j);
//预处理跳表
fa[j][0]=t;
for(int k=1;k<=25;k++){
fa[j][k]=fa[fa[j][k-1]][k-1];//倍增
}
}
}
}
}
int lca(int a,int b){
if(dep[a]<dep[b])swap(a,b);
//先让高dep跳到和低dep同一层
for(int k=25;k>=0;k--){
if(dep[fa[a][k]]>=dep[b])a=fa[a][k];//哨兵在此起作用
}
if(a==b)return a;//返回哪个都可以此时a和b表示的是同一个编号
//如果两个编号不相等,找最近公共祖先
for(int k=25;k>=0;k--){
if(fa[a][k]!=fa[b][k]){
a=fa[a][k];
b=fa[b][k];
}
}
return fa[a][0];
}
d f s 序函数 : dfs序函数: dfs序函数:
void dfs(int u,int fa){
dep[u]=dep[fa]+1;
f[u][0]=fa;dfn[u]=++timestamp;
for(int k=1;k<=20;k++)f[u][k]=f[f[u][k-1]][k-1];
for(int i=h[u];i!=-1;i=ne[i]){
int j=e[i];
if(j==fa)continue;
dfs(j,u);
}
}
边栏推荐
- Install the laser of ROS_ scan_ Problems encountered in match library (I)
- SSL==证书相关概念
- Since 2019, you must have stopped using this marketing strategy
- Lucifer 98 life record ing
- Typescript from getting started to mastering (XXII) namespace namespace (I)
- Malloc C language
- SQL窗口函数
- How to understand "page storage management scheme"
- 大厂们终于无法忍受“加一秒”了,微软谷歌Meta等公司提议废除闰秒
- [introduction to C language] zzulioj 1031-1035
猜你喜欢

Zhihuijun, a genius of Huawei, made a modular mechanical keyboard, which caused an earthquake in the geek circle. Netizens: This is the real customization

Function pointer and callback function

Since 2019, you must have stopped using this marketing strategy

HCIP BGP

小马智行进军前装量产,从自研域控制器入手?

1985-2020 (8 Editions) global surface coverage download and introduction

Note: restframe work records many to one tables, how to serialize in that table (reverse query)

Malloc C language

Data mining -- code implementation of association analysis example (Part 2)

Mmdetection preliminary use
随机推荐
The return value of the function is the attention of the pointer, the local variables inside the static limit sub function, and how the pointer to the array represents the array elements
Function pointer and callback function
When array is used as a function parameter, it is better to use the array size as a function parameter
Shopify seller: EDM marketing should be combined with salesmartly to easily get the conversion rate
flink-sql 如何设置 sql执行超时时间
C declaration and initialization and assignment
Value transmission and address transmission of C language, pointer of pointer
The function parameters of the new features of ES6 are assigned initial values and rest parameters
Extended operator of new features in ES6
路西法98-生活记录ing
The const keyword of ES6 declares variables
Some problems about pointers
Typescript from introduction to proficiency (XXIV) using import syntax
Connection broken by 'readtimc rt-443): read timed out (read timeout=l5)“)‘: /pac
SQL window function
C language to achieve three chess game (detailed explanation)
1985-2020(8个版次)全球地表覆盖下载与介绍
How to understand clock cycle and formula CPU execution time = number of CPU clock cycles / dominant frequency
Lucifer 98 life record ing
Common methods of lodash Library