Skip to content

Commit bf2a09d

Browse files
committed
[BOJ]줄세우기/ Gold3/ 45min
https://www.acmicpc.net/problem/2252
1 parent cdc0a41 commit bf2a09d

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def solution(s):
2+
answer = 0
3+
opps = 1
4+
5+
if s[0] == "+" :
6+
opps = 1
7+
answer = int(s[1:])
8+
elif s[0] == "-" :
9+
opps = 0
10+
answer = int(s[1:]) * (-1)
11+
else :
12+
answer = int(s)
13+
14+
15+
return answer

Hongjoo/백준/줄세우기.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import sys
2+
from collections import deque
3+
4+
def topology_sort() :
5+
result = []
6+
q = deque()
7+
8+
# 1. indegree= 0인 노드를 큐에 삽입
9+
for i in range(1,n+1) :
10+
if indegree[i] == 0:
11+
q.append(i)
12+
13+
# 2. 큐가 빌 때 까진 반복
14+
while q :
15+
# 2-1 큐에서 원소 꺼내기
16+
now = q.popleft()
17+
result.append(now) # resut == queue 꺼내는 순서
18+
# 2-2 해당 노드와 연결된 노드 (outdegree)들의 진입 차수에서 1 빼기
19+
for j in graph[now] :
20+
indegree[j] -=1
21+
#2-3. 새롭게 indegree=0 인 노드를 큐에 삽입
22+
if indegree[j] == 0 :
23+
q.append(j)
24+
25+
# 3.위상정렬을 수행한 결과 출력
26+
for i in result :
27+
print(i,end = " ")
28+
29+
n, m = map(int, sys.stdin.readline().split())
30+
#graph 만들기
31+
indegree = [0] * (n+1) # indegree list
32+
graph = [[] for _ in range(n+1)]
33+
for i in range(m):
34+
a,b = map(int , sys.stdin.readline().split())# a앞 -> b뒤
35+
graph[a].append(b)
36+
indegree[b] += 1
37+
38+
39+
topology_sort()

0 commit comments

Comments
 (0)