My first instinct was just BFS from 0 and count wrong-direction edges.
Model the problem as a tree rooted at node 0 and use DFS/BFS to traverse from node 0. For each edge, if it points away from node 0 (parent to child), it must be reversed; if it points toward node 0 (child to parent), it's already correct. Count the number of edges that need reversal.
Pro tip: Clarify that the graph is a tree when undirected, so there are no cycles and exactly n-1 edges. This simplifies the problem to a single traversal, and you can solve it in O(n) time.
Restate the problem: Given a directed graph that is a tree when undirected, we need to reverse the minimum number of edges so that every node can reach node 0. This means all edges must be directed toward node 0.
Use DFS or BFS starting from node 0 to traverse the tree. Since the graph is a tree, we can treat it as undirected for traversal, but we must consider the original direction of each edge.
During traversal, for each edge from current node u to neighbor v, if the original edge is directed from u to v (i.e., away from node 0), it needs to be reversed. If it's directed from v to u (toward node 0), it's already correct. Increment a counter for each reversal needed.
After traversing all nodes, the counter holds the minimum number of edges to reverse. Return this count as the answer.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.