Clean Python 3, BFS O(N)
Make an undirected graph and perform BFS start from 0.\nThen just check reverse roads while BFS.\nTime: O(N)\nSpace: O(N)\n\nimport collections\nclass Solution:
Make an undirected graph and perform BFS start from 0.\nThen just check reverse roads while BFS.\nTime: `O(N)`\nSpace: `O(N)`\n```\nimport collections\nclass Solution:\n    def minReorder(self, n: int, connections: List[List[int]]) -> int:\n        graph = collections.defaultdict(list)\n        visited = set([0])\n        for u, v in connections:\n            graph[u].append(v)\n            graph[v].append(u)\n        connections = set(map(tuple, connections))\n        queue = collections.deque([0])\n        changes = 0\n        while queue:\n            u = queue.popleft()\n            for v in graph[u]:\n                if v not in visited:\n                    changes += (u, v) in connections\n                    queue.append(v)\n                    visited.add(v)\n        return changes\n```
[]