-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlca.cpp
More file actions
76 lines (65 loc) · 1.27 KB
/
Copy pathlca.cpp
File metadata and controls
76 lines (65 loc) · 1.27 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
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define X first
#define Y second
int n, LG;
vector<vector<int>> adj;
int timer;
vector<int> tin, tout;
vector<vector<int>> up;
void dfs(int v, int p)
{
tin[v] = ++timer;
up[v][0] = p;
for (int i = 1; i <= LG; ++i)
up[v][i] = up[up[v][i - 1]][i - 1];
for (int u : adj[v])
{
if (u != p)
dfs(u, v);
}
tout[v] = timer; // take care of this line
}
bool is_ancestor(int u, int v)
{
return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int lca(int u, int v)
{
if (is_ancestor(u, v))
return u;
if (is_ancestor(v, u))
return v;
for (int i = LG; i >= 0; --i)
{
if (!is_ancestor(up[u][i], v))
u = up[u][i];
}
return up[u][0];
}
void preprocess(int root)
{
tin.resize(n + 1);
tout.resize(n + 1);
timer = 0;
LG = ceil(log2(n)) + 1;
up.assign(n, vector<int>(LG + 1));
dfs(root, root);
}
void solve()
{
}
int main()
{
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#endif
int tc = 1;
// cin>>tc;
for (int i = 1; i <= tc; ++i)
{
solve();
}
}