We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents e7f0c35 + cdf695d commit 12ccadfCopy full SHA for 12ccadf
live11/test119/문제1/박희경.py
@@ -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