Henu
개발냥발
Henu
전체 방문자
오늘
어제
  • 분류 전체보기 (411)
    • DevOps (52)
      • Kubernetes (19)
      • Docker (14)
      • AWS (3)
      • Nginx (4)
      • Linux (4)
      • ArgoCD (1)
      • CN (2)
      • NATS (0)
      • Git (5)
    • Back-End (30)
      • Django (18)
      • Spring (5)
      • JPA (1)
      • MSA (5)
    • CS (87)
      • SystemSoftware (20)
      • OS (25)
      • Computer Architecture (16)
      • Network (23)
      • Database (2)
    • Lang (21)
      • Java (9)
      • Python (4)
      • C# (8)
    • Life (12)
    • 블록체인 (2)
    • Algorithm (204)
      • BOJ (160)
      • 프로그래머스 (19)
      • LeetCode (4)
      • SWEA (1)
      • 알고리즘 문제 해결 전략 (8)
      • DS, algorithms (7)
      • Checkio (5)
    • IT (2)

블로그 메뉴

  • GitHub
  • 글쓰기
  • 관리자

공지사항

  • Free!

인기 글

태그

  • 다이나믹 프로그래밍
  • Network
  • 백트래킹
  • 프로그래머스
  • boj
  • docker
  • Kubernetes
  • django
  • BFS
  • DFS

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Henu

개발냥발

[백준 2623] 음악프로그램 C++
Algorithm/BOJ

[백준 2623] 음악프로그램 C++

2021. 6. 29. 10:45
 

2623번: 음악프로그램

첫째 줄에는 가수의 수 N과 보조 PD의 수 M이 주어진다. 가수는 번호 1, 2,…,N 으로 표시한다. 둘째 줄부터 각 보조 PD가 정한 순서들이 한 줄에 하나씩 나온다. 각 줄의 맨 앞에는 보조 PD가 담당한

www.acmicpc.net

위상 정렬


1. 문제 해결 아이디어

예제 입력을 그래프로 나타낸 것이다.

 

indegree를 활용한 위상 정렬로 풀었다.

 

입력을 받을 때 각 정점마다 indegree가 몇 개 인지 배열에 저장해준다.

 

위상 정렬 bfs를 수행하면서 현재 정점의 자식 정점들의 indegree를 하나씩 빼준다.

만약 자식 정점의 indegree가 0이 된다면 현재 정점을 queue에 넣어준다.

queue에 넣는다는 의미는 자신보다 먼저 수행되어야 할 정점이 없어서 현재 정점을 바로 실행해도 무방하다는 의미라고 볼 수 있다.

 


2. 코드

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
 
using namespace std;
 
int N, M;
int indegree[1001];
vector<int> Singer[1001];
 
void input(){
    cin >> N >> M;
    for(int k = 0; k < M; k++){
        int nos, sn, firstsn;
        cin >> nos >> firstsn;
        for(int i = 1; i < nos; i++){
            cin >> sn;
            Singer[firstsn].push_back(sn);
            indegree[sn]++;
            firstsn = sn;
        }
    }
}
 
void topological(){
    vector<int> result;
    
    queue<int> que;
    bool visited[1001];
    memset(visited,0, sizeof(visited));
    
    for(int i = 1; i <= N; i++){
        if(indegree[i] == 0){
            que.push(i);
            visited[i] = true;
        }
    }
    
    while(!que.empty()){
        int now = que.front();
        que.pop();
        
        result.push_back(now);
        
        for(int &next : Singer[now]){
            if(visited[next]) continue;
            
            if(--indegree[next] == 0){
                visited[next] = true;
                que.push(next);
            }
        }
    }
    
    for(int i = 1; i <= N; i++){
        if(indegree[i]){
            cout << 0;
            return;
        }
    }
    for(int &w : result){
        cout << w << "\n";
    }
}
 
int main(){
    input();
    topological();
    
    return 0;
}
Colored by Color Scripter
cs

 

'Algorithm > BOJ' 카테고리의 다른 글

[백준 4196] 도미노 C++  (1) 2021.07.01
[백준 1562] 계단 수 C++  (0) 2021.06.29
[백준 11266] 단절점 C++  (0) 2021.06.27
[백준 1208] 부분수열의 합2 C++  (0) 2021.06.27
[백준 1688] 지민이의 테러 C++  (0) 2021.06.27
    'Algorithm/BOJ' 카테고리의 다른 글
    • [백준 4196] 도미노 C++
    • [백준 1562] 계단 수 C++
    • [백준 11266] 단절점 C++
    • [백준 1208] 부분수열의 합2 C++

    티스토리툴바