Problem Description
Given a directed tree (i.e. if the edges were treated as undirected, they would form a tree) with n nodes (labeled 0 to n-1) and n-1 directed edges, we want to compute for every node i the minimum number of edge reversals required so that starting from node i it is possible to reach every other node following a sequence of directed edges.
Key Insights
- The underlying undirected structure is a tree.
- For each directed edge from u to v, if we travel from u to v, no reversal is needed; if we traverse in the opposite direction (v to u), we must “reverse” this edge (cost = 1).
- We can transform the problem into one on an undirected tree by assigning a cost (0 or 1) to each “direction” on every edge.
- First compute the total reversal cost for an arbitrary root (for example, node 0) with a DFS.
- Then use a re-rooting (tree DP) technique to compute the answer for every node in O(n) time.
- The re-rooting relation: when moving the root from node u to its neighbor v, update the cost as
- ans[v] = ans[u] + (if the edge from u to v was originally directed u->v then add 1, else subtract 1).
Space and Time Complexity
Time Complexity: O(n), since we process all nodes and edges using DFS twice (once for initial DFS and once for re-rooting). Space Complexity: O(n), for storing the tree (adjacency list) and recursion stack in the worst-case.
Solution
We will first build an undirected graph from the given edge list where each edge is stored twice:
- For an edge u->v, add (v, 0) to u’s list (indicating correct travel direction) and add (u, 1) to v’s list (indicating reversal if traveling from v to u). Next, perform a DFS from an arbitrary root (node 0) to compute the reversal cost needed when the root is 0. Then, use another re-root DFS: for an edge between u and v, if the original directed edge was from u to v (weight 0) then switching the root from u to v means that edge becomes “against” the new root’s requirement so we add 1; if the edge was originally v->u (weight 1), then switching reduces the cost by 1. This method calculates the answer for every node.