Practice/Microsoft/Leetcode 2858. Minimum Edge Reversals So Every Node Is Reachable
CodingOptional
You are given a tree represented as a directed graph with n nodes labeled from 0 to n-1. The graph has n-1 directed edges, and its underlying undirected structure forms a tree (connected, acyclic).
For each node in the tree, determine the minimum number of edge reversals needed so that starting from that node, you can reach every other node in the tree by following the directed edges.
An edge reversal means changing the direction of a directed edge. For example, if there's an edge from node u to node v, reversing it creates an edge from node v to node u.
Return an array answer of length n, where answer[i] is the minimum number of edge reversals required when starting from node i.
2 <= n <= 10^5edges.length == n - 1edges[i].length == 20 <= ui, vi < nui != viExample 1:
` Input: n = 3, edges = [[0,1],[1,2]] Output: [1,0,1] Explanation:
Example 2:
` Input: n = 4, edges = [[0,1],[0,2],[0,3]] Output: [0,1,1,1] Explanation:
Example 3:
Input: n = 5, edges = [[0,1],[1,2],[2,3],[3,4]] Output: [4,3,2,3,4] Explanation: This is a linear chain. From the middle node (2), we need 2 reversals total. As we move away from the center, the cost increases symmetrically.