diff options
| author | Gloria Mundi <gloria@gloria-mundi.eu> | 2024-11-16 01:24:14 +0100 |
|---|---|---|
| committer | Gloria Mundi <gloria@gloria-mundi.eu> | 2024-11-16 01:24:14 +0100 |
| commit | 98567ec798aa8ca2cfbcb85c774dd470f30e30d4 (patch) | |
| tree | 5113d5cc24d1ad5f93810b6442ce584a36950dc8 /content/graph/scc.cpp | |
| parent | ad3856a6b766087df0036de0b556f4700a6498c9 (diff) | |
| parent | 8d11c6c8213f46f0fa19826917c255edd5d43cb1 (diff) | |
mzuenni tests
Diffstat (limited to 'content/graph/scc.cpp')
| -rw-r--r-- | content/graph/scc.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/content/graph/scc.cpp b/content/graph/scc.cpp new file mode 100644 index 0000000..ac9a40b --- /dev/null +++ b/content/graph/scc.cpp @@ -0,0 +1,32 @@ +vector<vector<int>> adj, sccs; +int counter; +vector<bool> inStack; +vector<int> low, idx, s; //idx enthält Index der SCC pro Knoten. + +void visit(int v) { + int old = low[v] = counter++; + s.push_back(v); inStack[v] = true; + + for (auto u : adj[v]) { + if (low[u] < 0) visit(u); + if (inStack[u]) low[v] = min(low[v], low[u]); + } + + if (old == low[v]) { + sccs.push_back({}); + for (int u = -1; u != v;) { + u = s.back(); s.pop_back(); inStack[u] = false; + idx[u] = sz(sccs) - 1; + sccs.back().push_back(u); +}}} + +void scc() { + inStack.assign(sz(adj), false); + low.assign(sz(adj), -1); + idx.assign(sz(adj), -1); + sccs.clear(); + + counter = 0; + for (int i = 0; i < sz(adj); i++) { + if (low[i] < 0) visit(i); +}} |
