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

최근 글

태그

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

최근 댓글

인기 글

hELLO
HelloMinchan

처음처럼

[Baekjoon Online Judge] 백준 1766번 문제집(Python)
Deprecated

[Baekjoon Online Judge] 백준 1766번 문제집(Python)

2020. 6. 17. 12:37

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

[Baekjoon Online Judge] 백준 1766번 문제집

(Python)

(글쓴날 : 2020.06.17)

 


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

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


 

 

백준 1766번 문제집


1) 문제

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

 

1766번: 문제집

첫째 줄에 문제의 수 N(1 ≤ N ≤ 32,000)과 먼저 푸는 것이 좋은 문제에 대한 정보의 개수 M(1 ≤ M ≤ 100,000)이 주어진다. 둘째 줄부터 M개의 줄에 걸쳐 두 정수의 순서쌍 A,B가 빈칸을 사이에 두고 주

www.acmicpc.net


2) 풀이 과정

* 시간 복잡도 : O(n log n)

 

1번부터 N번까지 각 번호가 난이도를 의미하는 문제들이 주어지고, 주어지는 순서를 최우선으로 하되 가능하면 난이도순으로 문제를 해결하는 순서를 구하는 문제입니다.

 

저의 경우, 위상 정렬과 우선순위 큐를 적용하였으며, Python을 사용했습니다.

우선, 주어지는 정보들을 인접 리스트로 구현하였고, 진입 차수가 0인 문제들을 최소힙 기반의 우선순위 큐에 저장한 뒤, 위상 정렬을 적용하여 문제를 해결했습니다.


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
import sys, heapq
input = sys.stdin.readline
 
 
def topologicalSort():
    for _ in range(N):
        if not hq:
            return
        
        target = heapq.heappop(hq)
        sequence.append(target)
 
        for x in adjList[target]:
            indegree[x] -= 1
 
            if not indegree[x]:
                heapq.heappush(hq, x)
 
 
N, M = map(int, input().split())
 
sequence = []
indegree = [0] * (N + 1)
adjList = [[] for _ in range(N + 1)]
 
for _ in range(M):
    A, B = map(int, input().split())
 
    indegree[B] += 1
    adjList[A].append(B)
 
hq = []
for i in range(1, N + 1):
    if not indegree[i]:
        heapq.heappush(hq, i)
 
topologicalSort()
 
print(*sequence)

 

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

'Deprecated' 카테고리의 다른 글

[programmers] 프로그래머스 더 맵게(Python)  (0) 2020.06.19
[Baekjoon Online Judge] 백준 3665번 최종 순위(Python)  (0) 2020.06.17
[programmers] 프로그래머스 큰 수 만들기(Python)  (0) 2020.06.17
[programmers] 프로그래머스 문자열 압축(Python)  (0) 2020.06.17
[programmers] 프로그래머스 주식가격(Python)  (0) 2020.06.17
    'Deprecated' 카테고리의 다른 글
    • [programmers] 프로그래머스 더 맵게(Python)
    • [Baekjoon Online Judge] 백준 3665번 최종 순위(Python)
    • [programmers] 프로그래머스 큰 수 만들기(Python)
    • [programmers] 프로그래머스 문자열 압축(Python)
    HelloMinchan
    HelloMinchan
    Though you should not fear failure, You should do your very best to avoid it.

    티스토리툴바