forked from tanus786/CP-Codes-HackOctober-Fest-2023
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinaryLIfting.cpp
More file actions
100 lines (72 loc) · 1.5 KB
/
BinaryLIfting.cpp
File metadata and controls
100 lines (72 loc) · 1.5 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
ll n, m;
vector<vector<ll>> adj;
struct LCA{
vector<vector<ll>> lca;
ll maxN;
vi level;
void dfs(ll curr, ll par, ll lev){
lca[curr][0] = par;
level[curr] = lev;
for(auto a:adj[curr]){
if(a == par)continue;
dfs(a, curr, lev+1);
}
}
void init(ll source){//the sorce will be the root node
maxN = log2(n);
lca.resize(n+1, vector<ll>((maxN+1), -1));
level.resize(n+1);
dfs(source, -1, 0);
for(ll j=1; j<=maxN; j++){
for(ll i=0; i<n; i++){
if(lca[i][j-1] != -1){
ll par = lca[i][j-1];//one of my ancestors
lca[i][j] = lca[par][j-1];//one of the ancestors of ansestors
}
}
}
}
ll lcA(ll a, ll b){
if(level[a] > level[b]){
swap(a,b);
}
ll d = level[b] - level[a];
while(d > 0){
ll i = log2(d);
b = lca[b][i];
d -= (1ll<<i);
}
if(a == b){
return a;
}
for(ll i =maxN; i>=0; i--){
if(lca[a][i] != -1 && (lca[a][i] != lca[b][i])){
a = lca[a][i];
b = lca[b][i];
}
}
return lca[a][0];
}
ll dis(ll a, ll b){
return level[a] + level[b] - 2*level[lcA(a,b)];
}
};
void accept_ho_ja(){
cin>>n>>m;
ll x, y;
cin>>x>>y;
x--;y--;
adj.resize(n);
for(ll i=0; i<m; i++){
ll a, b;
cin>>a>>b;
a--;
b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
LCA lc;
lc.init(0);//we will pass the root node
// bug(lc.level[lcA(x,y)]);
cout<<lc.dis(x,y);//calculate the distance between the nodes
}