-
Notifications
You must be signed in to change notification settings - Fork 1
/
H-topsort.py
64 lines (43 loc) · 1.23 KB
/
H-topsort.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
60
61
62
63
64
from collections import defaultdict
import sys
sys.setrecursionlimit(100000)
def dfs(a, vert, used, prev):
used[vert] = True
prev[vert] = True
for value in a[vert]:
if (not used[value] and dfs(a, value, used, prev)) or prev[value]:
return True
prev[vert] = False
return False
def find_cycle(a, n):
used = [False] * n
prev = [False] * n
for i in range(n):
if not used[i] and dfs(a, i, used, prev):
return True
return False
def dfs1(vert, a, used, result):
used[vert] = True
for value in a[vert]:
if not used[value]:
dfs1(value, a, used, result)
result.append(vert + 1)
def topological_sort(a, n, result):
used = [False] * n
for i in range(n):
if not used[i]:
dfs1(i, a, used, result)
a = defaultdict(list)
result = []
with open('topsort.in', 'r') as f:
n, m = map(int, f.readline().split())
for i in range(m):
u, v = map(int, f.readline().split())
a[u - 1].append(v - 1)
if find_cycle(a, n):
with open('topsort.out', 'w') as f:
print(-1, file=f)
else:
topological_sort(a, n, result)
with open('topsort.out', 'w') as f:
print(*reversed(result), file=f)