Skip to content

Commit

Permalink
2024-03-13
Browse files Browse the repository at this point in the history
  • Loading branch information
H0ngJu committed Mar 13, 2024
1 parent dc35c20 commit f77be4e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions H0ngJu/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
| ์ฐจ์‹œ | ๋‚ ์งœ | ๋ฌธ์ œ์œ ํ˜• | ๋งํฌ | ํ’€์ด |
| :---: | :--------: | :------: | :-------------------------------------------------------------------------: | :-------------------------------------------------: |
| 1์ฐจ์‹œ | 2024.03.05 | ํ | [ํ”„๋กœ์„ธ์Šค](https://school.programmers.co.kr/learn/courses/30/lessons/42587) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/151 |
| 4์ฐจ์‹œ | 2024.03.13 | ํž™ | [๋ฌธ์ œ์ง‘](https://www.acmicpc.net/problem/1766) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/158 |

---
32 changes: 32 additions & 0 deletions H0ngJu/ํž™/๋ฌธ์ œ์ง‘.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import sys
import heapq

n, m = map(int, sys.stdin.readline().rstrip().split())

graph = [[] for _ in range(n+1)]
inDegree = [0 for _ in range(n+1)]
q = []
answer = []

# ์ž…๋ ฅ๋ฐ›์•„์„œ ๋„ฃ๊ธฐ
for _ in range(m):
p1, p2 = map(int, sys.stdin.readline().rstrip().split())
graph[p1].append(p2) # p1์€ p2์™€ ์—ฐ๊ฒฐ๋œ ๋ฌธ์ œ
inDegree[p2] += 1 # ๊ฐ„์„  ์ถ”๊ฐ€

# ์ง„์ž…์ฐจ์ˆ˜๊ฐ€ 0์ด๋ฉด ํ์— ๋„ฃ๊ธฐ
for i in range(1, n+1):
if inDegree[i] == 0:
heapq.heappush(q, i)

# answer์— ๋„ฃ๊ณ , ๊ฐ„์„  ์ œ๊ฑฐ
while q:
prob = heapq.heappop(q)
answer.append(prob)
for i in graph[prob]: # ๊ฐ„์„  ์ œ๊ฑฐ & ์ง„์ž…์ฐจ์ˆ˜ 0์ธ ๊ฒƒ๋“ค ์ฒ˜๋ฆฌ
inDegree[i] -= 1
if inDegree[i] == 0:
heapq.heappush(q, i)

for result in answer:
print(result, end=" ")

0 comments on commit f77be4e

Please sign in to comment.