Graph Theory37 sections · 1633 units
Open in Course

Up Pass (Propagating parent contributions)

(Parent contributions)

The second DFS propagates contributions from the parent side. For each node vv with parent uu, you compute up[v]: the answer contribution from everything outside vv's subtree.

To get up[v], start with up[u] (what uu sees from its parent side), then add contributions from all of uu's children except vv. This gives vv the full picture of the tree beyond its own subtree.

The root has no parent, so up[root] = 0. Every other node inherits and extends its parent's upup value. This pass also runs in O(n)O(n) time and uses O(n)O(n) space.