Skip to content
Snippets Groups Projects
Commit a7543847 authored by Bruno Freitas Tissei's avatar Bruno Freitas Tissei
Browse files

Add LCA

parent bba9ae81
No related branches found
No related tags found
No related merge requests found
/**
* Lowest Common Ancestor - LCA
*
* Complexity (Time):
* preprocess -> O(n log n)
* query -> O(log n)
* Complexity (Space): O(n + m + n log n)
*
*
* OBS: * = return sum path to LCA
* ** = return max value on path to LCA
*/
vector<ii> graph[MAX];
int h[MAX];
int par[MAX][MAXLOG], cost[MAX][MAXLOG];
// Perform DFS while filling h, par, and cost
void dfs(int v, int p = -1, int c = 0) {
par[v][0] = p;
//*** cost[v][0] = c;
if (p + 1)
h[v] = h[p] + 1;
for (int i = 1; i < MAXLOG; i++)
if (par[v][i - 1] + 1) {
par[v][i] = par[par[v][i - 1]][i - 1];
//*** cost[v][i] += cost[v][i - 1] + cost[par[v][i - 1]][i - 1];
}
for (auto u : graph[v])
if (p != u.fi)
dfs(u.fi, v, u.se);
}
// Preprocess tree with rooted at v
void preprocess(int v) {
memset(par, -1, sizeof par);
memset(cost, 0, sizeof cost);
dfs(v);
}
// Return LCA between p and q
int query(int p, int q) {
//*** int ans = 0;
if (h[p] < h[q])
swap(p, q);
for (int i = MAXLOG - 1; i >= 0; i--)
if (par[p][i] + 1 && h[par[p][i]] >= h[q]) {
//* ans += cost[p][i];
//** ans = max(ans, cost[p][i]);
p = par[p][i];
}
if (p == q)
return p;
//*** return ans;
for (int i = MAXLOG - 1; i >= 0; i--)
if (par[p][i] + 1 && par[p][i] != par[q][i]) {
//* ans += cost[p][i] + cost[q][i];
//** ans = max(ans, max(cost[p][i], cost[q][i]));
p = par[p][i];
q = par[q][i];
}
return dp[p][0];
//* if (p == q) return ans;
//* else return ans + cost[p][0] + cost[q][0];
//** if (p == q) return ans;
//** else return max(ans, max(cost[p][0], cost[q][0]));
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment