문제
https://www.acmicpc.net/problem/2252
코드
import sys
from collections import deque
input=sys.stdin.readline
def sort():
queue=deque()
for i in range(1,n+1):
if parent[i]==0:
queue.append(i)
while queue:
x=queue.popleft()
result.append(x)
for i in array[x]:
parent[i]-=1
if parent[i]==0:
queue.append(i)
n,m=map(int, input().split())
parent=[0]*(n+1)
array=[[] for _ in range(n+1)]
for _ in range(m):
a,b=map(int, input().split())
array[a].append(b)
parent[b]+=1
result=[]
sort()
for i in result:
print(i ,end=' ')
'문제풀이 > 그래프' 카테고리의 다른 글
[파이썬] [그래프] Pro 방의 개수 (0) | 2022.02.20 |
---|---|
[파이썬] [그래프] Pro 순위 (0) | 2022.02.20 |
[파이썬] [그래프] 백준 1922 네트워크 연결 (0) | 2022.02.07 |
[파이썬] [그래프] 백준 1717 집합의 표현 (0) | 2022.02.07 |
[파이썬] [그래프] union-find, kruskal, topology sort (0) | 2022.02.07 |