1. 개요
2. 풀이
import sys
from collections import deque
def solution():
READLINE = lambda: sys.stdin.readline()
READ_INT_LIST = lambda: list(map(int, READLINE().split()))
N, M = READ_INT_LIST()
DEGREE = [0]*(N+1)
G = [[] for _ in range(N+1)]
def driver():
for _ in range(M):
intList = READ_INT_LIST()
degrees(intList[0], intList[1:])
result = toplogies()
if len(result)==N:
return "\n".join(map(str, result))
else:
return 0
def degrees(n, links):
for i in range(n-1):
parent, child = links[i], links[i+1]
DEGREE[child] += 1
G[parent].append(child)
def toplogies():
q = deque()
order = []
for n in range(1, N+1):
if DEGREE[n]==0:
q.append(n)
while q:
node = q.popleft()
order.append(node)
for nextNode in G[node]:
DEGREE[nextNode] -= 1
if DEGREE[nextNode]==0:
q.append(nextNode)
return order
return driver()
print(solution())
Leave a comment