1
from collections import defaultdict
2

3

4
class Solution:
5
def minReorder(self, n: int, connections: List[List[int]]) -> int:
6
count, stack, visited = 0, [0], set() # Add root node to stack
7
neighbours = defaultdict(list) # To store neighbours
8
adjacency = defaultdict(list) # To store adjacency
9
for i, j in connections:
10
adjacency[i].append(j)
11
neighbours[i].append(j)
12
neighbours[j].append(i)
13
while stack:
14
current = stack.pop()
15
if current in visited:
16
continue
17
else:
18
visited.add(current)
19
for i in neighbours[current]:
20
if i in visited:
21
continue
22
if current not in adjacency[i]:
23
count += 1
24
stack.append(i)
25
return count

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0