← All problemsSign in

Sum of LCA

CodeChefRating 2825Open on judge ↗

You are given a tree $T$ consisting of $N$ nodes, rooted at node $1$. The nodes are numbered $1, 2, \ldots, N$. $\text{dep}(x)$ is defined as the number of nodes on the path from the root node $1$ and the node $x$. Note that this means $\text{dep}(1) = 1$. $\text{lca}(x, y)$ is defined as the node $z$ with the largest value of $\text{dep}(z)$ that lies on both the paths $(1, x)$ and $(1, y)$. F

HINT LADDERno hints yet
L1 Observation
L2 Technique
L3 Approach
L4 Pseudo-code
🔒
L5 Full solution
L5 unlocks only if you insist twice
solution.cppC++17

CodeSearch Tutor

Hints, not spoilers — it won’t hand over the full solution unless you insist.

voice by Sarvam AI

Sign in to chat with the tutor and save your progress.

Sign in to start