Skip to content

Commit ace3876

Browse files
authored
Update LCA (with Time Traversal DFS).cpp
1 parent 795eb48 commit ace3876

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

LCA (with Time Traversal DFS).cpp

+15
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ void dfs(int k, int par, int lvl)
1616
tout[k]=tim;
1717
}
1818

19+
int walk(int u, int h)
20+
{
21+
for(int i=LG-1;i>=0;i--)
22+
{
23+
if((h>>i) & 1)
24+
u = parent[i][u];
25+
}
26+
return u;
27+
}
28+
1929
void precompute()
2030
{
2131
for(int i=1;i<LG;i++)
@@ -49,5 +59,10 @@ int LCA(int u, int v)
4959
return parent[0][u];
5060
}
5161

62+
int dist(int u, int v)
63+
{
64+
return level[u] + level[v] - 2 * level[LCA(u, v)];
65+
}
66+
5267
//Problem 1 (Dynamic Diameter): https://codeforces.com/problemset/problem/379/F
5368
//Solution 1: https://codeforces.com/contest/379/submission/45960185

0 commit comments

Comments
 (0)