-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlca.cpp
More file actions
65 lines (55 loc) · 1.32 KB
/
lca.cpp
File metadata and controls
65 lines (55 loc) · 1.32 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
int n, l;
vector<vector<int>> adj;
struct LCA{
vector<vector<int>> g;
vector<vector<int>> up;
vector<int> tin, tout, depth;
int timer = 0;
inline void init(int n){
n++;
g.resize(n);
tin.resize(n);
tout.resize(n);
depth.resize(n);
up.assign(n, vector<int>(31, -1));
}
LCA(){};
LCA(int n){
init(n);
}
inline void add_edge(int u, int v){
g[u].push_back(v);
g[v].push_back(u);
}
inline void dfs(int v, int p , int dis){
tin[v] = ++timer;
depth[v] = dis;
up[v][0] = p;
for (int i = 1; i < 31; i++){
up[v][i] = up[up[v][i - 1]][i - 1];
}
for (int u : g[v]){
if (u != p){
dfs(u, v , dis+1);
}
}
tout[v] = ++timer;
}
void dfs(int root = 0){
dfs(root, root , 0);
}
inline bool upper(int u, int v){
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
int lca(int u, int v){
if (upper(u, v)) return u;
if (upper(v, u)) return v;
for (int i = 30 ; i >= 0; i--){
if (!upper(up[u][i], v)) u = up[u][i];
}
return up[u][0];
}
inline int dist(int u, int v){
return depth[u] + depth[v] - 2 * depth[lca(u, v)];
}
};