For each path (u, v):
lca_node = lca(u, v)
cnt[u] += 1
cnt[v] += 1
cnt[lca_node] -= 2
if parent[lca_node] exists:
cnt[parent[lca_node]] -= 0 // or handle separately
Then run DFS to propagate: for each node v in post-order, for each child c of v: cnt[v] += cnt[c]. After DFS, cnt[v] holds the number of paths passing through v.
Time: for marking paths (assuming LCA), plus for DFS. Total: . This is much better than the naive approach. Space: for the cnt array plus for LCA structures.