-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask2.py
More file actions
53 lines (43 loc) · 1.19 KB
/
Copy pathtask2.py
File metadata and controls
53 lines (43 loc) · 1.19 KB
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
#task 2
from queue import PriorityQueue
task2_input = open('input2_1.txt','r')
task2_output = open('output2_1.txt','w')
def BFS(N, parent):
graph = {}
in_degree = [0] * (N + 1)
for i in range(1, N + 1):
graph[i] = []
for A, B in parent:
graph[A].append(B)
in_degree[B] += 1
queue = PriorityQueue()
for k in range(1, N + 1):
if in_degree[k] == 0:
queue.put(k)
lexicographical_order = []
while not queue.empty():
course = queue.get()
lexicographical_order.append(course)
for i in graph[course]:
in_degree[i] -= 1
if in_degree[i] == 0:
queue.put(i)
if len(lexicographical_order) == N:
return lexicographical_order
else:
return []
N, M = map(int, task2_input.readline().split())
Graph_ = [list(map(int, task2_input.readline().split())) for i in range(M)]
lexicographical_order = BFS(N, Graph_)
if lexicographical_order == []:
s = "IMPOSSIBLE"
#print(s)
task2_output.write(s)
else:
s = ''
for i in lexicographical_order:
s += str(i) + ' '
#print(s)
task2_output.write(s)
task2_input .close()
task2_output.close()