29 lines
591 B
Python
29 lines
591 B
Python
import heapq
|
|
import sys
|
|
input = sys.stdin.readline
|
|
|
|
N, M = map(int, input().split())
|
|
|
|
graph = [[] for _ in range(N+1)]
|
|
inDegree = [0]*(N+1)
|
|
for _ in range(M) :
|
|
u, v = map(int, input().split())
|
|
graph[u].append(v)
|
|
inDegree[v] += 1
|
|
|
|
pq = []
|
|
for i in range(1,N+1) :
|
|
if inDegree[i] == 0 :
|
|
heapq.heappush(pq, i)
|
|
|
|
result = []
|
|
while len(pq) :
|
|
current = heapq.heappop(pq)
|
|
result.append(str(current))
|
|
|
|
for next in graph[current] :
|
|
inDegree[next] -= 1
|
|
if inDegree[next] == 0 :
|
|
heapq.heappush(pq, next)
|
|
|
|
print(" ".join(result)) |