Reorder Routes to Make All Paths Lead to the City Zero
MediumGraphs
Decode it first. Clues, constraints, pattern and plan before any code.There are n cities numbered 0 to n - 1 and exactly n - 1 roads, so there is exactly one route between any two cities if you ignore direction. Every road is one-way: [a, b] lets you drive from a to b only. The capital is city 0. Return the minimum number of roads to reverse so that every city can drive to city 0.
n = 6, connections = [[0, 1], [1, 3], [2, 3], [4, 0], [4, 5]] → 3
(flip 0→1, 1→3 and 4→5)
n = 3, connections = [[1, 0], [2, 0]] → 0Constraints
2 <= n <= 5 * 10^4connections.length == n - 10 <= a, b < nanda != b