https://www.acmicpc.net/problem/2252
위상 정렬의 기본을 다루는 문제이다.
from collections import deque
import sys
input = sys.stdin.readline
stdCnt, compareNum = map(int, input().split())
compareList = [[] for _ in range(stdCnt + 1)]
degreeList = [0] * (stdCnt + 1)
for i in range(compareNum):
compA, compB = map(int, input().split())
compareList[compA].append(compB)
degreeList[compB] += 1
queue = deque()
for i in range(1, stdCnt + 1):
if(degreeList[i] == 0):
queue.append(i)
while queue:
cur = queue.popleft()
print(str(cur) + " ", end="")
for nextItem in compareList[cur]:
degreeList[nextItem] -= 1
if(degreeList[nextItem] == 0):
queue.append(nextItem)
광고
광고
'문제풀이' 카테고리의 다른 글
[PY] 2748 : 피보나치 수 2 (0) | 2025.04.05 |
---|---|
[PY] 1904 : 01타일 (0) | 2025.04.05 |
[PY] 2665 : 미로만들기 (0) | 2025.04.02 |
[PY] 3055 : 탈출 (0) | 2025.04.02 |
[PY] <!> 1432 : 그래프 수정 (0) | 2025.04.02 |