tarjan code
tarjan code
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
// Global variables
vector<int> adj[1000005]; // Adjacency list as array of vectors
int ids[1000005]; // Discovery times array
int low[1000005]; // Lowest reachable vertex id array
bool onStack[1000005]; // Tracks vertices in current SCC array
stack<int> st; // Stack for DFS
int id = 0; // Current discovery time
int sccCount = 0; // Count of SCCs
int n; // Number of vertices
// Found an SCC
if (ids[at] == low[at]) {
while (!st.empty()) {
int node = st.top();
st.pop();
onStack[node] = false;
low[node] = ids[at];
if (node == at) break;
}
sccCount++;
}
}
int findSCC() {
for (int i = 0; i < n; i++)
if (ids[i] == -1)
dfs(i);
return sccCount;
}
initialize(n);
// Input m edges
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
// Assuming 0-based indexing
adj[u].push_back(v);
}
return 0;
}