当前位置:网站首页>LCA board
LCA board
2022-07-29 04:05:00 【Snow_ raw】
LCA The board
Variable :
i n t int int f a [ N ] [ 25 ] ( The second dimension depends on N size , Jump watch ) fa[N][25]( The second dimension depends on N size , Jump watch ) fa[N][25]( The second dimension depends on N size , Jump watch )
i n t int int d e p [ N ] ( Point depth ) dep[N]( Point depth ) dep[N]( Point depth )
i n t int int h [ N ] , e [ M ] , n e [ M ] , i d x ( Adjacency list ) h[N],e[M],ne[M],idx( Adjacency list ) h[N],e[M],ne[M],idx( Adjacency list )
seek seek seek d f s dfs dfs Ordinal variable : Ordinal variable : Ordinal variable :
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]
The key function :
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;// sentry
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);
// Pre processing skip table
fa[j][0]=t;
for(int k=1;k<=25;k++){
fa[j][k]=fa[fa[j][k-1]][k-1];// Multiply
}
}
}
}
}
int lca(int a,int b){
if(dep[a]<dep[b])swap(a,b);
// Let's go high first dep Jump to and low dep Same floor
for(int k=25;k>=0;k--){
if(dep[fa[a][k]]>=dep[b])a=fa[a][k];// Sentinels work here
}
if(a==b)return a;// You can return to any one at this time a and b It means the same number
// If the two numbers are not equal , Find the nearest public ancestor
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 Order function : dfs Order function : dfs Order function :
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);
}
}
边栏推荐
- Pointer variables -printf%d and%p meaning
- The solution of porting stm32f103zet6 program to c8t6+c8t6 download program flash timeout
- STM32F103ZET6程序移植为C8T6+C8T6下载程序flash timeout的解决方案
- 初识C语言(3)
- JS realizes the function of one click Copy
- Some problems about pointers
- 优炫数据库有办法查到主集群每天传给备集群的日志量吗?
- 通过PWM呼吸灯和PWM控制直流电机来详细介绍TIM的输出比较功能
- Alibaba Font Icon Library Usage and update methods
- pat A1041 Be Unique
猜你喜欢

Typescript from getting started to mastering (XX) function generics

SQL窗口函数

Solve the delay in opening the console of Google browser

大厂们终于无法忍受“加一秒”了,微软谷歌Meta等公司提议废除闰秒

Nacos registry

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

Is the browser multi process or single process?

Ssl== certificate related concepts

Value transmission and address transmission of C language, pointer of pointer

【深度学习CPU(番外篇)——虚拟内存】
随机推荐
安装postgis时报找不到“POSTGIS_VERSION”这个函数
Typescript from entry to mastery (XVIII) joint type and type protection
MySQL第三篇
LDP --- 标签分发协议
[untitled]
LVS+KeepAlived高可用部署实战应用
Design of environment detection system based on STM32 and Alibaba cloud
The table of antd hides the pager when there is only one page
Install the laser of ROS_ scan_ Problems encountered in match library (I)
谁能详细说下mysqlRC下的半一致读和怎么样减少死锁概率?
Typescript from introduction to proficiency (XXIV) using import syntax
C语言实现三子棋游戏(详解)
First ALV program 2
基于STM32和阿里云的环境检测系统设计
Lua语言(stm32+2G/4G模块)和C语言(stm32+esp8266)从字符串中提取相关数据的方法-整理
SQL语句 关于字段转换怎么写
JS cookie usage
Function pointer and callback function
路西法98-生活记录ing
Object array merges elements according to a field