1466. Reorder Routes to Make All Paths Lead to the City Zero

Обновлено: 2024-03-12
1 мин
[]

Содержание

LeetCode problem 1466

class Solution:
    def minReorder(self, n: int, connections: List[List[int]]) -> int:
        def dfs(a: int, fa: int) -> int:
            return sum(c + dfs(b, a) for b, c in g[a] if b != fa)

        g = [[] for _ in range(n)]
        for a, b in connections:
            g[a].append((b, 1))
            g[b].append((a, 0))
        return dfs(0, -1)