Imagine you have a network of cities connected by one-way roads. The network has a special property: if all roads were bidirectional, it would form a perfect tree structure with no cycles!
You're given n cities labeled from 0 to n-1 and a list of directed roads. Your mission is to find the minimum number of road reversals needed so that from each city, you can reach every other city by following the directed roads.
An edge reversal changes a road from "City A โ City B" to "City B โ City A".
Goal: For each starting city i, calculate independently how many roads need to be reversed to make all other cities reachable from city i.
Input: Number of cities n and array edges where edges[i] = [u, v] represents a road from city u to city v.
Output: Array where answer[i] is the minimum reversals needed when starting from city i.
Input & Output
Constraints
- n == edges.length + 1
- 1 โค n โค 105
- edges[i].length == 2
- 0 โค ui, vi โค n - 1
- ui โ vi
- The input represents a tree (connected graph with n-1 edges)