Choosing Capital for Treeland
MediumRating 1700CodeforcesAdvanced Trees (BST, LCA, Tree DP)
Approach hintTry it first, then revealHide
Count the reversals for one root by giving each edge cost 0 along its direction and 1 against it. Moving the capital across a single edge changes the count by exactly 1, so a second DFS evaluates every root.