Minimum Edge Reversals So Every Node Is Reachable Problem
Description
LeetCode Problem 2858.
There is a simple directed graph with n nodes labeled from 0 to n - 1. The graph would form a tree if its edges were bi-directional.
You are given an integer n and a 2D integer array edges, where edges[i] = [u_i, v_i] represents a directed edge going from node u_i to node v_i.
An edge reversal changes the direction of an edge, i.e., a directed edge going from node u_i to node v_i becomes a directed edge going from node v_i to node u_i.
For every node i in the range [0, n - 1], your task is to independently calculate the minimum number of edge reversals required so it is possible to reach any other node starting from node i through a sequence of directed edges.
Return an integer array answer, where answer[i] is the minimum number of edge reversals required so it is possible to reach any other node starting from node i through a sequence of directed edges.
Example 1:

Input: n = 4, edges = [[2,0],[2,1],[1,3]]
Output: [1,1,0,2]
Explanation: The image above shows the graph formed by the edges.
For node 0: after reversing the edge [2,0], it is possible to reach any other node starting from node 0.
So, answer[0] = 1.
For node 1: after reversing the edge [2,1], it is possible to reach any other node starting from node 1.
So, answer[1] = 1.
For node 2: it is already possible to reach any other node starting from node 2.
So, answer[2] = 0.
For node 3: after reversing the edges [1,3] and [2,1], it is possible to reach any other node starting from node 3.
So, answer[3] = 2.
Example 2:

Input: n = 3, edges = [[1,2],[2,0]]
Output: [2,0,1]
Explanation: The image above shows the graph formed by the edges.
For node 0: after reversing the edges [2,0] and [1,2], it is possible to reach any other node starting from node 0.
So, answer[0] = 2.
For node 1: it is already possible to reach any other node starting from node 1.
So, answer[1] = 0.
For node 2: after reversing the edge [1, 2], it is possible to reach any other node starting from node 2.
So, answer[2] = 1.
Constraints:
2 <= n <= 10^5edges.length == n - 1edges[i].length == 20 <= u_i == edges[i][0] < n0 <= v_i == edges[i][1] < nu_i != v_i- The input is generated such that if the edges were bi-directional, the graph would be a tree.
Solution
Approach: Rerooting
Treat the graph as an undirected tree. For each original edge x -> y, store (y, 1) at x and (x, -1) at y: the weight is 1 when we walk along the arrow and -1 when we walk against it.
Root the tree at node 0. A first DFS counts the edges that point toward the root (those walked with weight -1); that is ans[0].
Moving the root from i to a neighbour j changes the answer by one: if the edge pointed from i to j (weight 1) it now has to be reversed, and if it pointed from j to i (weight -1) it no longer has to be. A second DFS therefore sets ans[j] = ans[i] + weight.
Complexity: time O(n), space O(n).
class Solution:
def minEdgeReversals(self, n: int, edges: List[List[int]]) -> List[int]:
ans = [0] * n
g = [[] for _ in range(n)]
for x, y in edges:
g[x].append((y, 1))
g[y].append((x, -1))
def dfs(i: int, fa: int):
for j, k in g[i]:
if j != fa:
ans[0] += int(k < 0)
dfs(j, i)
dfs(0, -1)
def dfs2(i: int, fa: int):
for j, k in g[i]:
if j != fa:
ans[j] = ans[i] + k
dfs2(j, i)
dfs2(0, -1)
return ans