[알고리즘 문제풀이] 문제집

황인권·2023년 3월 31일
0

알고리즘 문제풀이

목록 보기
30/81

문제 제목 : 문제집

문제 난이도 : 중

문제 유형 : 힙, 위상정렬

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=' ')
profile
inkwon Hwang

0개의 댓글