-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtarjan.py
29 lines (29 loc) · 977 Bytes
/
tarjan.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
from collections import defaultdict
class Solution:
def criticalConnections(self, n, connections):
G = defaultdict(list)
for a,b in connections:
G[a].append(b)
G[b].append(a)
time = 0
visited, disc, low = [0]*n, [0]*n, [0]*n
bridges, articulation = [], set()
def dfs(x, p):
nonlocal time
visited[x] = True
disc[x] = low[x] = time
time += 1
components = p>=0
for nei in G[x]:
if nei == p: continue
new = not visited[nei]
components += new
if new:
dfs(nei, x)
if low[nei] > disc[x]:
bridges.append([x, nei])
if low[nei] >= disc[x] and components>=2:
articulation.add(x)
low[x] = min(low[x], low[nei])
dfs(0, -1)
return bridges