-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
38 lines (35 loc) · 911 Bytes
/
Copy pathtest.py
File metadata and controls
38 lines (35 loc) · 911 Bytes
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
def findSCC(g: list[list[int]]):
n = len(g)
on_stack = [False] * n
stack = []
t = [-1] * n
low = [-1] * n
c = 0
scc = []
def dfs(u):
nonlocal c
on_stack[u] = True
stack.append(u)
t[u] = c
low[u] = c
c += 1
for v in g[u]:
if t[v] == -1:
dfs(v)
low[u] = min(low[v], low[u])
elif on_stack[v]:
low[u] = min(low[u], t[v])
if t[u] == low[u]:
component = []
while stack[-1] != u:
node = stack.pop()
on_stack[node] = False
component.append(node)
node = stack.pop()
on_stack[node] = False
component.append(node)
scc.append(component)
for source in range(n):
if t[source] == -1:
dfs(source)
return scc