-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcritical-connections-in-a-network.py
43 lines (31 loc) · 1.15 KB
/
critical-connections-in-a-network.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from typing import List, Iterator
class Solution:
def criticalConnections(
self, n: int, connections: List[List[int]]
) -> List[List[int]]:
adj_list: List[List[int]] = [[] for _ in range(n)]
for src, dst in connections:
adj_list[src].append(dst)
adj_list[dst].append(src)
visited = [False] * n
times = [0] * n
low_times = [0] * n
result = []
def dfs(pos: int, parent: int, time: int) -> int:
times[pos] = time
low_times[pos] = time
for neigh_pos in adj_list[pos]:
if not visited[neigh_pos]:
time += 1
visited[neigh_pos] = True
time = dfs(neigh_pos, pos, time)
if neigh_pos != parent:
low_times[pos] = min(low_times[pos], low_times[neigh_pos])
if times[pos] <= low_times[pos] and parent != -1:
result.append([pos, parent])
return time
for pos in range(n):
if not visited[pos]:
visited[pos] = True
dfs(pos, -1, 0)
return result