Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def topologicSort(graph): # граф ориентированный, в виде словаря, нападобии списка смежности
- resultArr = []
- visited = []
- while len(graph) != len(visited):
- for s1 in graph.keys():
- s1 = int(s1)
- for s2 in graph.values():
- if s1 in s2 or s1 in visited:
- break
- else:
- resultArr.append(s1)
- visited.append(s1)
- if len(visited) == len(graph):
- resultArr.extend(graph[s1])
- graph[s1] = [s1]
- return resultArr
- n, m = [int(i) for i in input().split()] # n - кол-во вершин, m - кол-во ребер
- graph = {}
- for i in range(m):
- first, last = [int(i) for i in input().split()]
- if first not in graph:
- graph[first] = []
- graph[first].append(last)
- print(topologicSort(graph))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement