-
Notifications
You must be signed in to change notification settings - Fork 7
/
5.py
59 lines (49 loc) · 1.47 KB
/
5.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import enum
import sys
from typing import (
List,
Dict,
)
class Colors(enum.Enum):
WHITE = 0
GRAY = 1
BLACK = 2
def _sort(
start_vertex: int,
adjacency: Dict[int, List[int]],
colors: List[Colors],
) -> List[int]:
stack_sort: List[int] = []
stack = [start_vertex]
while stack:
v: int = stack.pop()
v_idx: int = v - 1
if colors[v_idx] == Colors.WHITE:
colors[v_idx] = Colors.GRAY
stack.append(v)
vs: List[int] = adjacency[v]
for new_vertex in sorted(vs, reverse=True):
if colors[new_vertex - 1] == Colors.WHITE:
stack.append(new_vertex)
elif colors[v_idx] == Colors.GRAY:
colors[v_idx] = Colors.BLACK
stack_sort.append(v)
return stack_sort
def sort(adjacency: Dict[int, List[int]]) -> List[int]:
result: List[int] = []
colors: List[Colors] = [Colors.WHITE] * len(adjacency)
for i in range(1, len(adjacency) + 1):
result.extend(
_sort(start_vertex=i, adjacency=adjacency, colors=colors)
)
return list(reversed(result))
def main() -> None:
m, n = map(int, input().split())
adjacency: Dict[int, List[int]] = {v: [] for v in range(1, m + 1)}
for _ in range(n):
v1, v2 = map(int, sys.stdin.readline().rstrip().split())
adjacency[v1].append(v2)
result: List[int] = sort(adjacency)
print(*result)
if __name__ == "__main__":
main()