My first instinct was to just BFS from every node and count reversals each time, which technically works but is way too slow.
Model the tree with directed edges and compute for each node the number of reversals needed if it were the root. Use a two-pass DFS: first compute the cost for an arbitrary root, then reroot to propagate costs to children in O(N) time.
Pro tip: Clarify that the tree is directed but we can reverse edges; the optimal root minimizes the number of edges pointing toward it. Mention that the rerooting technique is a common pattern in tree DP problems and can be extended to other similar problems.
Restate the problem: given a directed tree, choose a root to minimize the number of edges that must be reversed so all edges point away from the root. Confirm that edges can be reversed at a cost of 1 each.
Pick any node (e.g., node 0) as the root. Perform a DFS to count how many edges are directed toward the root (i.e., need reversal) to make all edges point away from it. This gives the cost for that root.
Use a second DFS to propagate the cost to children. When moving the root from parent to child, the cost changes by +1 if the edge was originally directed parent→child (since it now points toward the new root), and -1 if it was child→parent (since it now points away).
After computing costs for all nodes, return the minimum value. This is the answer.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.