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!

인기 글

태그

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

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Henu

개발냥발

[백준 6543] 그래프의 싱크 C++
Algorithm/BOJ

[백준 6543] 그래프의 싱크 C++

2021. 6. 14. 21:04
 

6543번: 그래프의 싱크

각 테스트 케이스마다 한 줄에 걸쳐 bottom(G)의 모든 노드를 출력한다. 노드는 공백으로 구분해야 하며, 오름차순이어야 한다. 만약, bottom(G)가 공집합이면 빈 줄을 출력한다.

www.acmicpc.net

강한 연결 요소

 

코사라주 알고리즘


1. 문제 해결 아이디어

 

[백준 2150] Strongly Connected Component C++

2150번: Strongly Connected Component 첫째 줄에 두 정수 V(1 ≤ V ≤ 10,000), E(1 ≤ E ≤ 100,000)가 주어진다. 이는 그래프가 V개의 정점과 E개의 간선으로 이루어져 있다는 의미이다. 다음 E개의 줄에는 간..

hyeo-noo.tistory.com

 

이전에 포스팅 했던 문제에서 사용한 '코사라주 알고리즘'을 조금 변형했다.

 

DFS를 통해서 가장 늦게 stack에 들어간 순서를 구하고

간선의 방향이 모두 바뀐 rev_graph에서 dfs를 수행하면서 SCC를 구한다.

 

SCC를 구할 때 각 정점이 몇 번째 SCC인지도 같이 기록한다.

 

rev_graph를 dfs할 때 다음 정점을 이미 방문했고

다음 정점이 현재 SCC에 속한 정점이 아닌 다른 SCC에 속한 정점이라면

다음 정점이 속한 SCC를 초기화 시키면 된다.


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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <bits/stdc++.h>
 
#define pii pair<int, int>
using namespace std;
 
int V, E, dtime = 1;
vector<int> Graph[5001];
vector<int> rev_Graph[5001];
vector<int> SCC[5001];
vector<int> rev_search_seq;
int SCC_id[5001];
 
bool visited[10001], rev_visited[10001];
 
void dfs(int now){
    
    visited[now] = true;
    
    for(int i = 0; i < Graph[now].size(); i++){
        int next = Graph[now][i];
        if(visited[next]) continue;
        
        dfs(next);
    }
    
    rev_search_seq.push_back(now);
}
 
void rev_dfs(int now, int id){
    
    rev_visited[now] = true;
    SCC[id].push_back(now);
    SCC_id[now] = id;
    
    for(int i = 0; i < rev_Graph[now].size(); i++){
        int next = rev_Graph[now][i];
        if(rev_visited[next]){
            if(SCC_id[next] != id){
                SCC[SCC_id[next]].clear();
            }
            continue;
        }
        
        rev_dfs(next, id);
    }
    
}
 
void solve(){
    for(int i = 1; i <= V; i++){
        if(visited[i]) continue;
        dfs(i);
    }
    
    int scc_id = 0;
    
    for(int i = rev_search_seq.size()-1; i >= 0; i--){
        int now = rev_search_seq[i];
        if(rev_visited[now]) continue;
        rev_dfs(now, scc_id);
        scc_id++;
    }
    
    vector<int> res;
    
    for(int i = 0; i < V; i++){
        for(auto &w : SCC[i]){
            res.push_back(w);
        }
    }
    
    sort(res.begin(), res.end());
    
    for(auto &w : res){
        cout << w << " ";
    }
    cout << "\n";
}
 
void init(){
    for(int i = 0; i < 5001; i++){
        Graph[i].clear();
        rev_Graph[i].clear();
        SCC[i].clear();
    }
    rev_search_seq.clear();
    memset(SCC_id, 0, sizeof(SCC_id));
    memset(visited, 0, sizeof(visited));
    memset(rev_visited, 0, sizeof(rev_visited));
}
 
void input(){
    int a, b;
    while(true){
        cin >> V;
        if(!V) break;
        cin >> E;
        for(int i = 0; i < E; i++){
            cin >> a >> b;
            Graph[a].push_back(b);
            rev_Graph[b].push_back(a);
        }
        
        solve();
        
        init();
    }
}
 
int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    
    input();
    
    return 0;
}
 
Colored by Color Scripter
cs

 

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

[백준 2162] 선분 그룹 C++  (0) 2021.06.20
[백준 1405] 미친 로봇 C++  (0) 2021.06.15
[백준 1708] 볼록 껍질 C++  (0) 2021.06.13
[백준 17387] 선분 교차 2 C++  (1) 2021.06.11
[백준 2150] Strongly Connected Component C++  (0) 2021.06.10
    'Algorithm/BOJ' 카테고리의 다른 글
    • [백준 2162] 선분 그룹 C++
    • [백준 1405] 미친 로봇 C++
    • [백준 1708] 볼록 껍질 C++
    • [백준 17387] 선분 교차 2 C++

    티스토리툴바