Метод двоичного подъёма
Перейти к навигации
Перейти к поиску
struct Graph {
vector<vector<int>> graph, ancestor;
vector<int> l, r, depth;
int timer = 0;
Graph(int vertexCount) :
graph(vertexCount),
ancestor(vertexCount, vector<int>(20)),
l(vertexCount),
r(vertexCount),
depth(vertexCount) {}
void addEdge(int a, int b) {
graph[a].push_back(b);
graph[b].push_back(a);
}
void dfs(int v, int parent) {
depth[v] = (v == parent ? 0 : depth[parent] + 1);
l[v] = ++timer;
ancestor[v][0] = parent;
for (int i = 1; i < ancestor[v].size(); i++)
ancestor[v][i] = ancestor[ancestor[v][i - 1]][i - 1];
for (int to : graph[v])
if (!l[to])
dfs(to, v);
r[v] = ++timer;
}
void prepare(int root) {
dfs(root, root);
}
bool isAncestor(int a, int b) {
return l[a] <= l[b] && r[b] <= r[a];
}
int lca(int a, int b) {
if (isAncestor(a, b))
return a;
if (isAncestor(b, a))
return b;
for (int i = ancestor[a].size() - 1; i >= 0; i--)
if (!isAncestor(ancestor[a][i], b))
a = ancestor[a][i];
return ancestor[a][0];
}
int distance(int a, int b) {
int l = lca(a, b);
int da = depth[a] - depth[l];
int db = depth[b] - depth[l];
return da + db;
}
};
Ссылки
- e-maxx.ru — Наименьший общий предок. Нахождение за O(log N) (метод двоичного подъёма)
- neerc.ifmo.ru/wiki — Метод двоичного подъёма
- algorithmica.org — Корневые деревья
- usaco.guide — Euler Tour Technique
- usaco.guide — Binary Jumping
- indy256/codelibrary/java/graphs/lca/LcaSparseTable.java (несмотря на название, приведён код метода двоичного подъёма)
- ADJA/algos/Graphs/LCABinary.cpp