mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
07e991d553
* ci(pre-commit): Add pep8-naming to `pre-commit` hooks (#7038) * refactor: Fix naming conventions (#7038) * Update arithmetic_analysis/lu_decomposition.py Co-authored-by: Christian Clauss <cclauss@me.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * refactor(lu_decomposition): Replace `NDArray` with `ArrayLike` (#7038) * chore: Fix naming conventions in doctests (#7038) * fix: Temporarily disable project euler problem 104 (#7069) * chore: Fix naming conventions in doctests (#7038) Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
55 lines
1.3 KiB
Python
55 lines
1.3 KiB
Python
from __future__ import annotations
|
|
|
|
|
|
def dfs(u):
|
|
global graph, reversed_graph, scc, component, visit, stack
|
|
if visit[u]:
|
|
return
|
|
visit[u] = True
|
|
for v in graph[u]:
|
|
dfs(v)
|
|
stack.append(u)
|
|
|
|
|
|
def dfs2(u):
|
|
global graph, reversed_graph, scc, component, visit, stack
|
|
if visit[u]:
|
|
return
|
|
visit[u] = True
|
|
component.append(u)
|
|
for v in reversed_graph[u]:
|
|
dfs2(v)
|
|
|
|
|
|
def kosaraju():
|
|
global graph, reversed_graph, scc, component, visit, stack
|
|
for i in range(n):
|
|
dfs(i)
|
|
visit = [False] * n
|
|
for i in stack[::-1]:
|
|
if visit[i]:
|
|
continue
|
|
component = []
|
|
dfs2(i)
|
|
scc.append(component)
|
|
return scc
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# n - no of nodes, m - no of edges
|
|
n, m = list(map(int, input().strip().split()))
|
|
|
|
graph: list[list[int]] = [[] for i in range(n)] # graph
|
|
reversed_graph: list[list[int]] = [[] for i in range(n)] # reversed graph
|
|
# input graph data (edges)
|
|
for i in range(m):
|
|
u, v = list(map(int, input().strip().split()))
|
|
graph[u].append(v)
|
|
reversed_graph[v].append(u)
|
|
|
|
stack: list[int] = []
|
|
visit: list[bool] = [False] * n
|
|
scc: list[int] = []
|
|
component: list[int] = []
|
|
print(kosaraju())
|