Skip to content

Commit

Permalink
Merge pull request #158 from AlgoLeadMe/4-H0ngJu
Browse files Browse the repository at this point in the history
4-H0ngJu
  • Loading branch information
H0ngJu authored Mar 25, 2024
2 parents 301cbcc + 865b02b commit bc817de
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 @@ -5,5 +5,6 @@
| 1์ฐจ์‹œ | 2024.03.05 | ํ | [ํ”„๋กœ์„ธ์Šค](https://school.programmers.co.kr/learn/courses/30/lessons/42587) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/151 |
| 2์ฐจ์‹œ | 2024.03.07 | ํ | [๋‹ค๋ฆฌ๋ฅผ ์ง€๋‚˜๋Š” ํŠธ๋Ÿญ](https://school.programmers.co.kr/learn/courses/30/lessons/42583) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/153 |
| 3์ฐจ์‹œ | 2024.03.10 | ํž™ | [N๋ฒˆ์งธ ํฐ ์ˆ˜](https://www.acmicpc.net/problem/2075) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/156 |
| 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 bc817de

Please sign in to comment.