当前位置:网站首页>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);
}
}
边栏推荐
- UCOS任务切换过程
- Typescript from entry to mastery (XVIII) joint type and type protection
- OPENSQL快速学习
- 通过PWM呼吸灯和PWM控制直流电机来详细介绍TIM的输出比较功能
- STM32F103ZET6程序移植为C8T6+C8T6下载程序flash timeout的解决方案
- [kvm] common commands
- The const keyword of ES6 declares variables
- String template of ES6 new features and methods to simplify objects and functions
- 3.解决Pycharm报错Unresolved reference ‘selenium‘ Unresolved reference ‘webdriver‘
- Wechat applet monitors sliding events on the screen
猜你喜欢
Zhihuijun, a genius of Huawei, made a modular mechanical keyboard, which caused an earthquake in the geek circle. Netizens: This is the real customization
【深度学习CPU(番外篇)——虚拟内存】
Ma Zhixing entered the mass production of front loading, starting with the self-developed domain controller?
MPU6050
SSL==证书相关概念
Big manufacturers finally can't stand "adding one second", and companies such as Microsoft, Google meta propose to abolish leap seconds
为什么opengauss启动的时候这么多的unknown?
Install the laser of ROS_ scan_ Problems encountered in match library (I)
Three tier architecture of enterprise network
UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0x90 in position 614: ordinal not in range(128)
随机推荐
Meeting notice of OA project (Query & whether to attend the meeting & feedback details)
Deep understanding of browser caching mechanism (HTTP)
Object array merges elements according to a field
rman不标记过期备份
Problems encountered in vscode connection SSH
Arrow function of new features of ES6
Configmap configuration and secret encryption
Deconstruction assignment of new features of ES6
Typescript from getting started to mastering (XXII) namespace namespace (I)
Codeforces Round #810 (Div. 2) D. Rain (线段树差分)
Solution: module 'xlrd' has no attribute 'open_ Error reporting of workbook '
When defining an array, the size must be constant
Blood cases caused by < meta charset=UTF-8> -- Analysis of common character codes
Analysis of new retail o2o e-commerce model
力扣面试题17.04 消失的数字||260.只出现一次的数字(内含位运算知识点)
路由 知识
Lucifer 98 life record ing
AssertionError(“Torch not compiled with CUDA enabled“)
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
LDP --- 标签分发协议