Skip to content

Commit 12ccadf

Browse files
Merge pull request #736 from gmlrude/main
[박희경] 119차 라이브 코테 제출
2 parents e7f0c35 + cdf695d commit 12ccadf

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import sys
2+
import heapq
3+
from collections import *
4+
5+
input = sys.stdin.readline
6+
7+
n, m = map(int, input().split())
8+
graph = defaultdict(list)
9+
idx = [0] * (n + 1) # 진입 차수
10+
11+
for _ in range(m):
12+
a, b = map(int, input().split()) # a번은 b번 보다 먼저
13+
graph[a].append(b)
14+
idx[b] += 1
15+
16+
heap = []
17+
for i in range(1, n + 1):
18+
if idx[i] == 0:
19+
heapq.heappush(heap, i)
20+
21+
res = []
22+
while heap:
23+
now = heapq.heappop(heap)
24+
res.append(now)
25+
for nxt in graph[now]:
26+
idx[nxt] -= 1
27+
if idx[nxt] == 0:
28+
heapq.heappush(heap, nxt)
29+
30+
print(*res)
31+

0 commit comments

Comments
 (0)