cnt = array of size n, initialized to 0
For each path (u, v):
lca_node = lca(u, v)
cnt[u] += 1
cnt[v] += 1
cnt[lca_node] -= 2
function propagate(node):
for each child c of node:
propagate(c)
cnt[node] += cnt[c]
propagate(root)
Output cnt[]
Time: with binary lifting for LCA, for DFS propagation.
The key: marking is per path, propagation is total. Much better than walking each path individually, which would be .