Graph Theory37 sections · 1633 units
Open in Course

Core Insight - Parent Decrement

(Fixing the formula)

Without the parent decrement at cnt[parent[lca(u, v)]] -= 11, counts leak to nodes above the LCA and give wrong answers. The standard 22-point marking (cnt[u] += 11, cnt[v] += 11, cnt[lca] -= 22) works for counting paths that include the LCA itself. But if you want each edge or node on the path counted once, you need the 44-point version.

The parent decrement cancels the extra count that would otherwise propagate upward past the LCA during DFS summation. This is a subtle detail that many implementations miss.