위상정렬 알고리즘
예제 입력 1
3 2
1 3
2 3
정답코드
import sys
from collections import deque
input=sys.stdin.readline
def solve():
N,M=map(int,input().split())
arr=[[] for _ in range(N+1)]
count=[0]*(N+1)
queue=deque()
for _ in range(M):
A,B=map(int,input().split())
count[B]+=1
arr[A].append(B)
for i in range(1,N+1):
if count[i]==0:
queue.append(i)
while queue:
now=queue.popleft()
for num in arr[now]:
count[num]-=1
if count[num]==0:
queue.append(num)
print(now, end=' ')
solve()
https://www.acmicpc.net/problem/2252
'IT > coding study' 카테고리의 다른 글
[programmers] 크레인 인형뽑기 게임 (python) (0) | 2022.01.10 |
---|---|
[acmicpc] 8911. 거북이(python) (0) | 2021.12.27 |
[acmicpc] 19238. 스타트 택시(python) (0) | 2021.10.24 |
[acmicpc] 21610. 마법사 상어와 비바라기(python) (0) | 2021.10.20 |
[programmers] 베스트앨범(python) (0) | 2021.03.18 |