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) |
'코딩테스트 준비' 카테고리의 다른 글
스택 문제 stack (0) | 2021.02.26 |
---|---|
위상 정렬! (0) | 2021.02.26 |
누적 합 알고리즘! - itertools의 accumulate() (0) | 2021.02.25 |
재귀, 분할정복 - 백준 쿼드트리 (0) | 2021.02.25 |
LIS, DP - 백준 - 가장 긴 바이토닉 부분 수열 (0) | 2021.02.24 |