-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCA.cpp
More file actions
50 lines (45 loc) · 849 Bytes
/
Copy pathLCA.cpp
File metadata and controls
50 lines (45 loc) · 849 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// [最近共同祖先.cpp]
#include<bits/stdc++.h>
using namespace std;
#define INT long long int
const INT mxn=1e5+5;
INT deep[mxn];//距離根節點的距離(根節點距離為0)
INT p[mxn][35];
vector<INT> v[mxn];
void dfs(INT n,INT lst){
for(INT i:v[n]){
if(i==lst)continue;
p[i][0]=n;
deep[i]=deep[n]+1;
dfs(i,n);
}
}
void build(INT n){
for(INT i=1;i<20;i++){
for(INT j=0;j<n;j++){
p[j][i]=p[p[j][i-1]][i-1];
}
}
}
INT LCA(INT a,INT b){//最近共同祖先
if(deep[a]<deep[b])swap(a,b);
if(deep[a]>deep[b]){
INT dif=deep[a]-deep[b];
for(INT i=0;i<20;i++){
if(dif&1)a=p[a][i];
dif>>=1;
}
}
if(a==b)return a;
for(INT i=19;i>=0;i--){
if(p[a][i]!=p[b][i]){
a=p[a][i];
b=p[b][i];
}
}
return p[a][0];
}
INT len(INT a,INT b){//a,b的距離
INT lca=LCA(a,b);
return deep[a]+deep[b]-deep[lca]*2;
}