Skip to content

Commit 01f64a0

Browse files
committed
[BOJ] 줄 세우기 / 골드3 / 65분 힌트
https://www.acmicpc.net/problem/2252
1 parent 499fe19 commit 01f64a0

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from collections import deque
2+
3+
4+
def topology_sort(graph, inDegree):
5+
queue = deque()
6+
# 위상정렬 결과를 담을 리스트
7+
result = []
8+
9+
# 진입차수가 0인 정점을 큐에 추가
10+
for num in range(len(inDegree)):
11+
if inDegree[num] == 0:
12+
queue.append(num + 1)
13+
14+
while queue:
15+
now = queue.popleft()
16+
result.append(now)
17+
18+
# 진입차수 업데이트
19+
for vertex in graph[now - 1]:
20+
inDegree[vertex - 1] -= 1
21+
# 진입차수가 0인 정점을 큐에 추가
22+
if inDegree[vertex - 1] == 0:
23+
queue.append(vertex)
24+
25+
return result
26+
27+
28+
# 정점, 간선의 개수
29+
N, M = map(int, input().split())
30+
31+
# 진입차수
32+
inDegree = [0] * N
33+
# 그래프
34+
graph = [[] for _ in range(N)]
35+
36+
for i in range(M):
37+
first, second = map(int, input().split())
38+
# 그래프 변경
39+
graph[first - 1].append(second)
40+
# 진입차수 증가
41+
inDegree[second - 1] += 1
42+
43+
result = topology_sort(graph, inDegree)
44+
for ans in result:
45+
print(ans, end=" ")

0 commit comments

Comments
 (0)