aboutsummaryrefslogtreecommitdiff
path: root/Graph/lca.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Graph/lca.cpp')
-rw-r--r--Graph/lca.cpp11
1 files changed, 7 insertions, 4 deletions
diff --git a/Graph/lca.cpp b/Graph/lca.cpp
index d02598f..38d972e 100644
--- a/Graph/lca.cpp
+++ b/Graph/lca.cpp
@@ -1,5 +1,5 @@
-struct lca {
- int d[MN], L[MN][20];
+namespace lca {
+ int d[MX], L[MX][20];
void dfs(vector<int> * G, int u = 1, int p = 0) {
d[u] = d[p] + 1, L[u][0] = p;
for (int i = 0; i < 18 && L[u][i]; i++) L[u][i + 1] = L[L[u][i]][i];
@@ -7,9 +7,12 @@ struct lca {
}
int lca(vector<int> * G, int u, int v) {
if (d[u] > d[v]) swap(u, v);
- for (int i = 18; i >= 0; i--) if (d[v] - (1 << i) >= d[u]) v = L[v][i];
+ for (int i = 18; i >= 0; --i) if (d[v] - (1 << i) >= d[u]) v = L[v][i];
if (u == v) return u;
- for (int i = 18; i >= 0; i--) if (L[u][i] && L[u][i] != L[v][i]) u = L[u][i], v = L[v][i];
+ for (int i = 18; i >= 0; --i) if (L[u][i] && L[u][i] != L[v][i]) u = L[u][i], v = L[v][i];
return L[u][0];
}
+ inline int dist(vector<int> * G, int u, int v) {
+ return d[u]+d[v]-2*d[lca(G, u, v)];
+ }
}