-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlowest_common_ancestor.cpp
More file actions
35 lines (30 loc) · 944 Bytes
/
lowest_common_ancestor.cpp
File metadata and controls
35 lines (30 loc) · 944 Bytes
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
// Tarjan's off-line lowest common ancestors algorithm is an algorithm for
// computing lowest common ancestors for pairs of nodes in a tree, based on the
// union-find data structure.
// https://en.wikipedia.org/wiki/Tarjan%27s_off-line_lowest_common_ancestors_algorithm
// linear time
// A practical version
typedef struct node {
int v;
node *next;
} node;
// Find for a disjoint-set forest.
int Find(int x) {
if (Ances[x] == x) return x;
return Ances[x] = Find(Ances[x]);
}
void Tarjan(int u) {
color[u] = BLACK;
Ances[u] = u;
for (int i = 0; i < query[u].size(); ++i) {
// id[u][i] is the No. of query[u][i]
if (BLACK == color[query[u][i]]) LCA[id[u][i]] = Find(query[u][i]);
}
for (node *p = children[u]; p != NULL; p = p->next) {
if (BLACK != [p->v]) {
Tarjan(p->v);
Ances[p->v] = u;
}
}
return;
}