-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLCA.cpp
More file actions
91 lines (86 loc) · 1.73 KB
/
LCA.cpp
File metadata and controls
91 lines (86 loc) · 1.73 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
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define PI acos(-1)
#define pb push_back
#define int long long int
#define pi pair<int, int>
#define pii pair<int, pi>
#define fir first
#define sec second
#define DEBUG 0
#define MAXN 100001
#define mod 1000000007
int n;
vector<int> adj[MAXN];
namespace lca
{
int l, timer;
vector<int> tin, tout, depth;
vector<vector<int>> up;
void dfs(int v, int p)
{
tin[v] = ++timer;
up[v][0] = p;
for (int i = 1; i <= l; i++)
up[v][i] = up[up[v][i - 1]][i - 1];
for (auto const &u : adj[v])
{
if (p == u)
continue;
depth[u] = depth[v] + 1;
dfs(u, v);
}
tout[v] = ++timer;
}
bool is_ancestor(int u, int v)
{
return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int binary_lifting(int u, int v)
{
if (is_ancestor(u, v))
return u;
if (is_ancestor(v, u))
return v;
for (int i = l; i >= 0; --i)
if (!is_ancestor(up[u][i], v))
u = up[u][i];
return up[u][0];
}
void init()
{
tin.resize(n);
tout.resize(n);
depth.resize(n);
timer = 0;
l = ceil(log2(n));
up.assign(n, vector<int>(l + 1));
dfs(0, 0);
}
int dist(int s, int v)
{
int at = binary_lifting(s, v);
return (depth[s] + depth[v] - 2 * depth[at]);
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for (int i = 0; i < n - 1; i++)
{
int a, b;
cin >> a >> b;
a--, b--;
adj[a].pb(b);
adj[b].pb(a);
}
lca::init();
return 0;
}