Files
Algorithm/백준/2252.py
2026-04-11 11:55:44 +09:00

27 lines
519 B
Python

import sys
from collections import deque
input = sys.stdin.readline
N, M = map(int, input().split())
A = [[] for _ in range(N+1)]
indegree = [0] * (N+1)
for i in range(M):
S, E = map(int, input().split())
A[S].append(E)
indegree[E] += 1
queue = deque()
for i in range(1, N+1):
if indegree[i] == 0:
queue.append(i)
while queue:
now = queue.popleft()
print(now, end=' ')
for next in A[now]:
indegree[next]-=1
if indegree[next] == 0:
queue.append(next)