HelloMinchan
처음처럼
HelloMinchan
LinkedIn
전체 방문자
오늘
어제
  • 분류 전체보기 (306)
    • Backend (4)
      • NestJS (1)
      • Express (1)
      • Spring (2)
    • Infrastructure (1)
      • AWS (1)
    • Frontend (1)
      • Next.js (1)
    • Language & Runtime (4)
      • Java (2)
      • Node.js (2)
    • Computer Science (8)
      • Computer Networks (3)
      • Operating Systems (4)
      • OOP (1)
    • 독서 (4)
      • 데이터 중심 애플리케이션 설계 (3)
      • 객체지향의 사실과 오해 (1)
    • 회고 (4)
      • Project (2)
      • Career (2)
    • Deprecated (280)

채널

  • GitHub
  • LinkedIn

최근 글

태그

  • programmers
  • 코딩
  • 프로그래머스Python
  • back-end
  • front-end
  • Database
  • 개발자
  • 알고스팟
  • 백준C++
  • 백준Go
  • 백준
  • 프로그래머스C++
  • 데이터베이스
  • 백준Python
  • Algospot
  • 프로그래머스
  • 백엔드
  • 알고스팟Python
  • 프로그래밍
  • Baekjoon Online Judge

최근 댓글

인기 글

hELLO
HelloMinchan

처음처럼

[Baekjoon Online Judge] 백준 2252번 줄 세우기(Python)
Deprecated

[Baekjoon Online Judge] 백준 2252번 줄 세우기(Python)

2020. 6. 16. 22:10

© 2020 All Rights Reserved. 주식회사 스타트링크

[Baekjoon Online Judge] 백준 2252번 줄 세우기

(Python)

(글쓴날 : 2020.06.16)

 


* Baekjoon Online Judge, 백준 2252번 문제 Python 언어 풀이입니다.

* 소스 코드의 저작권은 글쓴이에게 있습니다.


 

 

백준 2252번 줄 세우기


1) 문제

문제 링크 : https://www.acmicpc.net/problem/2252

 

2252번: 줄 세우기

첫째 줄에 N(1≤N≤32,000), M(1≤M≤100,000)이 주어진다. M은 키를 비교한 회수이다. 다음 M개의 줄에는 키를 비교한 두 학생의 번호 A, B가 주어진다. 이는 학생 A가 학생 B의 앞에 서야 한다는 의미이��

www.acmicpc.net


2) 풀이 과정

* 시간 복잡도 : O(V+E)

 

N명의 학생들 중 일부 학생들의 키를 비교한 순서가 주어질 때, 가능한 전체 학생들의 키 순서를 구하는 문제입니다.

 

저의 경우, 위상 정렬을 적용하였고, Python을 사용했습니다.

주어지는 일부 학생들의 키 순서를 인접 리스트에 구현한 뒤, 위상 정렬을 적용하여 가능한 전체 학생들의 키 순서 중 하나를 구해 문제를 해결했습니다.


3) 코드

 

* Python 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from collections import deque
import sys
input = sys.stdin.readline
 
 
def topologicalSort():
    for _ in range(N):
        if not dq:
            return
        
        target = dq.popleft()
        ans.append(target)
 
        for v in adjList[target]:
            indegree[v] -= 1
 
            if not indegree[v]:
                dq.append(v)
                
 
N, M = map(int, input().split())
 
ans = []
indegree = [0] * (N + 1)
adjList = [[] for _ in range(N + 1)]
 
for _ in range(M):
    A, B = map(int, input().split())
 
    adjList[A].append(B)
    indegree[B] += 1
 
dq = deque()
for i in range(1, N + 1):
    if not indegree[i]:
        dq.append(i)
 
topologicalSort()
 
print(*ans)

 

저작자표시 비영리 변경금지 (새창열림)

'Deprecated' 카테고리의 다른 글

[programmers] 프로그래머스 주식가격(Python)  (0) 2020.06.17
[Baekjoon Online Judge] 백준 1005번 ACM Craft(Python)  (0) 2020.06.16
[Baekjoon Online Judge] 백준 2887번 행성 터널(Python)  (0) 2020.06.16
[Baekjoon Online Judge] 백준 1647번 도시 분할 계획(Python)  (0) 2020.06.16
[programmers] 프로그래머스 124 나라의 숫자(Python)  (0) 2020.06.16
    'Deprecated' 카테고리의 다른 글
    • [programmers] 프로그래머스 주식가격(Python)
    • [Baekjoon Online Judge] 백준 1005번 ACM Craft(Python)
    • [Baekjoon Online Judge] 백준 2887번 행성 터널(Python)
    • [Baekjoon Online Judge] 백준 1647번 도시 분할 계획(Python)
    HelloMinchan
    HelloMinchan
    Though you should not fear failure, You should do your very best to avoid it.

    티스토리툴바