aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony Wang2020-08-21 15:21:02 -0500
committerAnthony Wang2020-08-21 15:21:02 -0500
commit4f1ef57c5c5fcb959adc8af422394ef4d8527b28 (patch)
tree5da28e5a75be3f8364c51beda6fb8734f7f74040
parent0f80ef68d7560daf2bda471b35915f37d43d8b34 (diff)
Wrap everything into a struct
-rw-r--r--Graph/tarjan_scc.cpp47
1 files changed, 23 insertions, 24 deletions
diff --git a/Graph/tarjan_scc.cpp b/Graph/tarjan_scc.cpp
index a812aba..7f89859 100644
--- a/Graph/tarjan_scc.cpp
+++ b/Graph/tarjan_scc.cpp
@@ -1,25 +1,24 @@
-int cnt, scc_num, scc[MN], in[MN], low[MN];
-stack<int> s;
-bitset<MN> ins;
-
-void tarjan(int u) {
- low[u] = in[u] = cnt++;
- s.push(u);
- ins[u] = 1;
- for (int v : G[u]) {
- if (in[v] == -1) tarjan(v), low[u] = min(low[u], low[v]);
- else if (ins[v]) low[u] = min(low[u], in[v]);
- }
- if (low[u] == in[u]) {
- while (1) {
- int x = s.top(); s.pop();
- scc[x] = scc_num, ins[x] = 0;
- if (x == u) break;
- }
- ++scc_num;
- }
+struct tarjan {
+ int cnt, scc_num, scc[MN], in[MN], low[MN];
+ stack<int> s;
+ bitset<MN> ins;
+ void tarjan(vector<int> * G, int u) {
+ low[u] = in[u] = cnt++, ins[u] = 1; s.push(u);
+ for (int v : G[u]) {
+ if (in[v] == -1) tarjan(G, v), low[u] = min(low[u], low[v]);
+ else if (ins[v]) low[u] = min(low[u], in[v]);
+ }
+ if (low[u] == in[u]) {
+ while (1) {
+ int x = s.top(); s.pop();
+ scc[x] = scc_num, ins[x] = 0;
+ if (x == u) break;
+ }
+ ++scc_num;
+ }
+ }
+ void find_scc(vector<int> * G) {
+ memset(scc, -1, sizeof scc), memset(in, -1, sizeof in);
+ for (int u = 1; u <= N; ++u) if (scc[u] == -1) tarjan(G, u);
+ }
}
-
-
-memset(scc, -1, sizeof scc), memset(in, -1, sizeof in);
-for (int u = 1; u <= N; ++u) if (scc[u] == -1) tarjan(u);