-
Strongly Connected Component - 2150Algorithm/BOJ 2020. 1. 20. 05:3612345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485//백준 SCC - 2150#include <iostream>#include <vector>#include <queue>#include <stack>#include <algorithm>using namespace std;int V, E;vector<vector<int>> adj;vector<int> discovered;vector<int> sccId;priority_queue<pair<int, vector<int>>> pq;stack<int> st;int sccCounter = 0, counter = 0;bool operator<(pair<int, vector<int>> &p1, pair<int, vector<int>> &p2){return p1.first < p2.first;}int scc(int here){int ret = discovered[here] = counter++;int there;st.push(here);for (int i = 0; i < adj[here].size(); i++){there = adj[here][i];if (discovered[there] == -1)ret = min(ret, scc(there));else if (sccId[there] == -1)ret = min(ret, discovered[there]);}if (ret == discovered[here]){vector<int> v;while(true){int t = st.top(); st.pop();sccId[t] = sccCounter;v.push_back(t);if(t == here)break;}++sccCounter;sort(v.begin(), v.end());pq.push(make_pair(-1*v[0], v));}return ret;}void tarjanScc(){for (int i = 1; i <= V; i++)if (discovered[i] == -1)scc(i);}int main(){cin >> V >> E;adj = vector<vector<int>>(V + 1);sccId = discovered = vector<int>(V + 1, -1);st = stack<int>();int x, y;for (int i = 0; i < E; i++){cin >> x >> y;adj[x].push_back(y);}tarjanScc();cout << sccCounter << endl;while (!pq.empty()){vector<int> v = pq.top().second;pq.pop();for(int i =0 ; i<v.size(); i++)cout << v[i] << " ";cout << "-1" << endl;}}
cs https://www.acmicpc.net/problem/2150
2150번: Strongly Connected Component
첫째 줄에 두 정수 V(1≤V≤10,000), E(1≤E≤100,000)가 주어진다. 이는 그래프가 V개의 정점과 E개의 간선으로 이루어져 있다는 의미이다. 다음 E개의 줄에는 간선에 대한 정보를 나타내는 두 정수 A, B가 주어진다. 이는 A번 정점과 B번 정점이 연결되어 있다는 의미이다. 이때 방향은 A->B가 된다.
www.acmicpc.net
Strongly Connected Component
SCC문제 tarjan's SCC algorithm을 이용했습니다.
'Algorithm > BOJ' 카테고리의 다른 글
효율적인 해킹 - 1325 (0) 2020.02.11 최소 스패닝 트리 - 1197 (0) 2020.01.30 최대공약수와 최소공배수 - 2609 (0) 2020.01.20 단절선 - 11400 (1) 2020.01.10 단절점 - 11266 (0) 2020.01.10