-
Notifications
You must be signed in to change notification settings - Fork 153
/
lowest_common_ancestor_lca.cpp
91 lines (81 loc) · 2.29 KB
/
lowest_common_ancestor_lca.cpp
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
/**
* Description: LCA (Finds the lowest common ancestor of two nodes in a tree)
* Usage: LCA constructor O(Nlg(N)), query O(lg(N))
* Source: https://github.com/dragonslayerx
*/
template<typename value_type> class RMQ {
vector<int> a;
vector<vector<value_type> > rmq;
int size;
int log2(int x){
int i = 0, n = 1;
while (x >= n) {
i++; n <<= 1;
}
i--;
return i;
}
public:
RMQ() {}
RMQ(vector<value_type> &a) : a(a) {
size = log2(a.size()) + 1;
rmq.resize(size, vector<value_type> (a.size()));
construct(a);
}
void construct(vector<value_type> &array){
for (int i = 0; i < array.size(); i++) rmq[0][i] = i;
for (int k = 1; k < size; k++) {
for (int i = 0; i < a.size(); i++) {
int length = 1 << (k - 1);
if (i + length >= a.size() || a.at(rmq[k - 1][i]) < a.at(rmq[k - 1][i + length])){
rmq[k][i] = rmq[k - 1][i];
} else {
rmq[k][i] = rmq[k - 1][i + length];
}
}
}
}
int query(int i, int j){
int range = j - i + 1;
int logr = log2(range);
if (a[rmq[logr][i]] < a[rmq[logr][j - (1 << logr) + 1]])
return rmq[logr][i];
else
return rmq[logr][j - (1 << logr) + 1];
}
};
class LCA {
typedef vector<vector<int> > tree;
vector<int> E, H, L;
RMQ<int> R;
tree T;
vector<bool> isvisited;
void euler_tour(int node, int level){
isvisited[node] = 1;
E.push_back(node);
L.push_back(level);
for (vector<int>::iterator i = T[node].begin(); i != T[node].end(); i++) {
if (!isvisited[*i]) {
euler_tour(*i, level + 1);
E.push_back(node);
L.push_back(level);
}
}
}
public:
LCA(tree &T, int root): T(T){
isvisited.resize(T.size());
H.resize(T.size(), -1);
euler_tour(root, 0);
for (int i = 0; i < E.size(); i++) {
if (H[E[i]] == -1)
H[E[i]] = i;
}
R = RMQ<int>(L);
}
int lca(int a, int b){
if (H[a] > H[b]) swap(a, b);
int index = R.query(H[a], H[b]);
return E[index];
}
};