코딩테스트 준비
위상 정렬!
언어 수집가
2021. 2. 26. 16:24
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from collections import deque | |
n, m = map(int, sys.stdin.readline().split()) | |
ind = [0]*(n+1) | |
graph = [[0] for _ in range(n+1)] | |
for i in range(m): | |
a, b = map(int, sys.stdin.readline().split()) | |
graph[a].append(b) | |
ind[b] += 1 | |
queue = deque() | |
for i in range(1, n+1): | |
if ind[i] == 0: | |
queue.append(i) | |
while queue: | |
now = queue.popleft() | |
print(now, end=' ') | |
for i in graph[now]: | |
ind[i] -= 1 | |
if ind[i] == 0: | |
queue.append(i) |