백준 1766 - 문제집 (Python)
Updated:
우선 순위가 있기 때문에 queue대신 heapq를 사용했습니다.
전체 코드
import sys
import heapq
input = sys.stdin.readline
N, M = map(int, input().rstrip().split())
counts = [0 for i in range(32001)]
graph = [[] for i in range(32001)]
q = []
for i in range(M):
a, b = map(int, input().rstrip().split())
graph[a].append(b)
counts[b] += 1
for i in range(1, N + 1):
if counts[i] == 0:
heapq.heappush(q, i)
answer = []
while q:
tmp = heapq.heappop(q)
answer.append(tmp)
for i in graph[tmp]:
counts[i] -= 1
if counts[i] == 0:
heapq.heappush(q, i)
print(*answer)