From e470f14dcadd825963181411df99a9c69ab8a853 Mon Sep 17 00:00:00 2001 From: andeley Date: Thu, 31 Oct 2019 10:12:49 -0500 Subject: [PATCH] Create Tarjan.cpp --- Graph Programs/Tarjan.cpp | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Graph Programs/Tarjan.cpp diff --git a/Graph Programs/Tarjan.cpp b/Graph Programs/Tarjan.cpp new file mode 100644 index 0000000..83d03ab --- /dev/null +++ b/Graph Programs/Tarjan.cpp @@ -0,0 +1,42 @@ + +Dado un grafo dirigido halla las componentes fuertemente conexas (SCC). + +const int MAX = 100005; +vector g[MAX]; +bitset vis; +stack st; +int low[MAX], num[MAX], cont; +int compOf[MAX]; +int cantSCC; +int N, M; + +void tarjan(int u) { + low[u] = num[u] = cont++; + st.push(u); + vis[u] = true; + + for (int v : g[u]) { + if (num[v] == -1) + tarjan(v); + if (vis[v]) + low[u] = min(low[u], low[v]); + } + + if (low[u] == num[u]) { + while (true) { + int v = st.top(); st.pop(); + vis[v] = false; + compOf[v] = cantSCC; + if (u == v) break; + } + cantSCC++; + } +} + +void init() { + cont = cantSCC = 0; + for (int i = 0; i <= N; i++) { + g[i].clear(); + num[i] = -1; //no visit + } +}