Graph Theory37 sections · 1633 units
Open in Course

Think First

(Subtree + rerooting)

This is the hardest rerooting problem so far. Think carefully:

1.1. Downward pass: What do you store for each node? (Hint: Max balance in subtree rooted at u, taking only parts with positive contribution)

2.2. Upward pass: When moving root from u to v, what 'up' value do you pass to v? (Hint: Contribution from u and everything except v's subtree)

3.3. Final answer at node u: How do you combine down and up? (Hint: u always contributes its color, then add positive parts only) Core idea: You can choose to exclude negative-balance subtrees. So you take max(00, child_contribution). Spend 22 minutes thinking.