-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlca.cpp
More file actions
92 lines (78 loc) · 2.47 KB
/
Copy pathlca.cpp
File metadata and controls
92 lines (78 loc) · 2.47 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
#include <bits/stdc++.h>
#include <iostream>
#define ll long long
ll Ceil(ll a, ll b) { return ((a / b) + (a % b != 0)); }
using namespace std;
void build_table(vector<ll> adj[], vector<vector<ll> >& par, vector<ll>& depth, int n, int D)
{ //par should be intailized by -1 and sent
queue<ll> q;
vector<bool> vis(n + 1, false);
q.push(1);
vis[1] = true;
while (!q.empty()) {
ll node = q.front();
q.pop();
for (auto neigh : adj[node]) {
if (!vis[neigh]) {
vis[neigh] = true;
depth[neigh] = depth[node] + 1;
par[neigh][0] = node;
q.push(neigh);
}
}
}
for (int d = 1; d <= D; d++) {
for (int i = 1; i <= n; i++) {
ll mid = par[i][d - 1];
if (mid != -1) {
par[i][d]=par[mid][d-1];
}
}
}
}
ll walk(ll i, ll k, ll D, vector<vector<ll> >& par) //d=log2(n)
{
for (int j = D; j>=0;j--)
if ((k & (1ll << j)) > 0)
i = par[i][j];
return i;
}
ll lca(ll i, ll j, vector<vector<ll> >& par, vector<ll>& depth, ll D)
{
if (depth[i] > depth[j]) {
i = walk(i, depth[i] - depth[j], D, par); //make their depth equal
}
if (depth[j] > depth[i]) {
j = walk(j, depth[j] - depth[i], D, par);
}
if (i == j)
return i;
for (int d = D; d >= 0; d--) {
if (par[i][d] != par[j][d]) {
i = par[i][d]; //i is a node just less than lca
j = par[j][d];
}
}
return par[i][0];
}
ll distance(ll i, ll j, vector<vector<ll> >& par, vector<ll>& depth, ll D)
{
return depth[i] + depth[j] - 2 * (depth[lca(i, j, par, depth, D)]);
}
int main()
{
ll n;
cin >> n;
vector<ll> adj[n + 1];
for (int i = 1; i <= n - 1; i++) {
ll a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
ll D = log2(n);
vector<vector<ll> > par(n + 1, vector<ll>(D + 1, -1)); //intialized by -1
vector<ll> depth(n + 1);
build_table(adj, par, depth, n, D);
//distance(i, j, par, depth, D)
}