Spent the first few minutes just making sure I understood the problem correctly, drew out the example on the whiteboard and traced through it manually.
Model the problem as a tree traversal from the given root, where each edge has a cost: 0 if it already points away from the root, and 1 if it points toward the root. Perform a DFS/BFS from the root, summing these costs to get the minimum number of reversals needed.
Pro tip: Clarify that the graph is a tree (n nodes, n-1 edges) and that edge directions are given; then emphasize that the optimal solution is to reverse exactly the edges that point toward the root in the rooted tree. This shows you understand the structure and avoid unnecessary complexity.
Confirm that the graph is a tree when ignoring directions, and that we need to reorient edges so all point away from the given root. Note that reversing an edge changes its direction, and we want the minimum number of such reversals.
Root the tree at the given root. For each edge, assign a cost: 0 if it already points away from the root (parent to child), and 1 if it points toward the root (child to parent). The total cost is the number of reversals needed.
Perform a DFS or BFS from the root, traversing edges in both directions (ignoring original direction for traversal). For each edge encountered, add its cost to a running total. This works because the tree has no cycles, so each edge is visited once.
After traversing all edges, the accumulated sum is the minimum number of reversals. Return this value.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.