-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #152 from AlgoLeadMe/7-ljedd2_
7 ljedd2
- Loading branch information
Showing
2 changed files
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |