BOJ - 2252 - 줄 세우기

Updated:

import sys
from collections import deque

def solution():
    N, M = map(int, sys.stdin.readline().split())
    graph = [[] for _ in range(N+1)]
    degree = [0] * (N+1)
    dq = deque()

    for _ in range(M):
        A, B = map(int, sys.stdin.readline().split())
        graph[A].append(B)
        degree[B] += 1

    for i in range(1, N + 1):
        if degree[i] == 0:
            dq.append(i)

    while dq:
        student = dq.popleft()
        for j in graph[student]:
            degree[j] -= 1
            if degree[j] == 0:
                dq.append(j)
        print(student, end=' ')

solution()

https://www.acmicpc.net/problem/2252

Categories:

Updated:

Leave a comment