1from collections import defaultdict234class Solution:5def minReorder(self, n: int, connections: List[List[int]]) -> int:6count, stack, visited = 0, [0], set() # Add root node to stack7neighbours = defaultdict(list) # To store neighbours8adjacency = defaultdict(list) # To store adjacency9for i, j in connections:10adjacency[i].append(j)11neighbours[i].append(j)12neighbours[j].append(i)13while stack:14current = stack.pop()15if current in visited:16continue17else:18visited.add(current)19for i in neighbours[current]:20if i in visited:21continue22if current not in adjacency[i]:23count += 124stack.append(i)25return count