https://www.acmicpc.net/problem/1766
시간 제한 : 2초
메모리 제한 : 128MB
import heapq
n, m = map(int, input().split())
array = [[] for i in range(n + 1)]
indegree = [0] * (n + 1)
heap = []
result = []
for _ in range(m):
x, y = map(int, input().split())
array[x].append(y)
indegree[y] += 1
for i in range(1, n + 1):
if indegree[i] == 0:
heapq.heappush(heap, i)
result = []
while heap:
data = heapq.heappop(heap)
result.append(data)
for y in array[data]:
indegree[y] -= 1
if indegree[y] == 0:
heapq.heappush(heap, y)
for i in result:
print(i, end=' ')