Skip to content

Commit 952be59

Browse files
author
hangyeol
committed
119차 1번 문제풀이
1 parent d2d90e3 commit 952be59

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import heapq
2+
import sys
3+
from collections import defaultdict
4+
5+
6+
def main():
7+
input = sys.stdin.readline
8+
9+
N, M = map(int, input().split())
10+
11+
problems = defaultdict(list)
12+
aheadResolveProblems = [0] * (N+1)
13+
14+
for i in range(M):
15+
A, B = map(int, input().split())
16+
problems[A].append(B)
17+
aheadResolveProblems[B] += 1
18+
19+
result = []
20+
heap = []
21+
22+
for i in range(1, N+1):
23+
if aheadResolveProblems[i] == 0:
24+
heapq.heappush(heap, i)
25+
26+
while heap:
27+
now = heapq.heappop(heap)
28+
result.append(now)
29+
30+
for next in problems[now]:
31+
aheadResolveProblems[next] -= 1
32+
if aheadResolveProblems[next] == 0:
33+
heapq.heappush(heap, next)
34+
35+
print(*result)
36+
37+
if __name__ == '__main__':
38+
main()
39+
40+
# 1234
41+
# 1. 4는 2보다 먼저 푸는 것이 좋음 -> 1 4 2 3
42+
# 2. 3은 1보다 먼저 푸는 것이 좋음 -> 3 1 4 2

0 commit comments

Comments
 (0)