Skip to content

Commit

Permalink
Merge pull request #152 from AlgoLeadMe/7-ljedd2_
Browse files Browse the repository at this point in the history
7 ljedd2
  • Loading branch information
LJEDD2 authored Sep 25, 2024
2 parents 016a501 + a25db22 commit 3fea77b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions LJEDD2/2024-2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
| 4차시 | 2024.07.27 | 그래프 | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/49189">가장 먼 노드</a> | [#4](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/133) |
| 5차시 | 2024.07.31 | 다이나믹프로그래밍 | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/43105">정수 삼각형</a> | [#5](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/139) |
| 6차시 | 2024.08.03 | 완전탐색 | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/87946">피로도</a> | [#6](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/141) |
| 7차시 | 2024.09.01 | 위상정렬 | <a href="https://www.acmicpc.net/problem/2252">줄 세우기</a> | [#7](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/152) |
28 changes: 28 additions & 0 deletions LJEDD2/2024-2/위상정렬/줄 세우기.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# BOJ2252 줄 세우기
from collections import deque

n, m = map(int, input().split())
graph = [[] for _ in range(n + 1)]
indegree = [0] * (n + 1) # 진입 차수 저장할 리스트

for i in range(m):
s,e = map(int, input().split())
graph[s].append(e) # zz
indegree[e] += 1 # 진입 차수 데이터를 저장

# B를 하기 위해 A라는 작업을 먼저 해야 하는 구조가 있을 때,
# 그 작업 순서를 구해주는 것 = 위상정렬

queue = deque()
for i in range(1, n + 1):
if not indegree[i]:
queue.append(i)

while queue: # 위상 정렬 수행 , 진입 차수 없애!
now = queue.popleft()
print(now, end=' ')

for next in graph[now]:
indegree[next] -= 1 # 진입 차수 0인 정점 큐에 삽입
if not indegree[next]:
queue.append(next)

0 comments on commit 3fea77b

Please sign in to comment.