Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 한글 깨짐
- 안드로이드 스튜디오
- BFS
- Kosaraju's algorithm
- 앱 이름 변경
- upper_bound
- 계산기
- 이분 탐색
- 앱
- scc
- c언어
- KMP
- 최단경로
- C++
- AlertDialog
- 알고리즘
- Parametric Search
- 안드로이드
- lower_bound
- 어플
Archives
- Today
- Total
소시지
Kosaraju's algorithm - 강한 연결 요소(SCC) 찾기 본문
SCC(Strongly Connected Component)란?
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 | #include <stdio.h> #include <vector> #include <stack> #include <set> #include <algorithm> using namespace std; int n, out_len; bool check[10001], check2[10001]; vector<int> r[10001], r2[10001]; stack<int> S, S2; set<int> out[10001]; void dfs(int x) { check[x] = true; for (auto it = r[x].begin(); it != r[x].end(); it++) if (check[*it] == false) dfs(*it); S.push(x); } void dfs2(int x, int w) { check2[x] = true; for (auto it = r2[x].begin(); it != r2[x].end(); it++) if (check2[*it] == false) dfs2(*it,w); out[w].insert(x); } int main() { int x, y, m; scanf("%d %d", &n, &m); for (int i = 1; i <= m; i++) { scanf("%d %d", &x, &y); r[x].push_back(y), r2[y].push_back(x); } for (int i = 1; i <= n; i++) if (check[i] == false) dfs(i); while (!S.empty()) { int x = S.top(); S.pop(); if (check2[x]) continue; dfs2(x,++out_len); } sort(out + 1, out + out_len + 1); printf("%d\n", out_len); for (int i = 1; i <= out_len; i++) { for (auto it = out[i].begin(); it != out[i].end(); it++) printf("%d ", *it); printf("-1\n"); } } | cs |
'C++ Algorithm' 카테고리의 다른 글
KMP 알고리즘 (0) | 2017.01.17 |
---|---|
이분 탐색의 응용(Parametric Search) (0) | 2017.01.16 |
upper_bound(First, Last, Val) / lower_bound(First, Last, Val) (0) | 2017.01.16 |
BFS (0) | 2017.01.16 |
Comments