15void print(
const std::vector<std::vector<int> > &a,
int V) {
16 for (
int i = 0; i < V; i++) {
18 std::cout <<
"i=" << i <<
"-->";
21 std::cout << j <<
" ";
24 std::cout << std::endl;
37void push_vertex(
int v, std::stack<int> *st, std::vector<bool> *vis,
38 const std::vector<std::vector<int> > &adj) {
40 for (
auto i = adj[v].begin(); i != adj[v].end(); i++) {
41 if ((*vis)[*i] ==
false) {
42 push_vertex(*i, st, vis, adj);
55void dfs(
int v, std::vector<bool> *vis,
56 const std::vector<std::vector<int> > &grev) {
59 for (
auto i = grev[v].begin(); i != grev[v].end(); i++) {
60 if ((*vis)[*i] ==
false) {
76int kosaraju(
int V,
const std::vector<std::vector<int> > &adj) {
77 std::vector<bool> vis(V,
false);
79 for (
int v = 0; v < V; v++) {
80 if (vis[v] ==
false) {
81 push_vertex(v, &st, &vis, adj);
85 std::vector<std::vector<int> > grev(V);
86 for (
int i = 0; i < V + 1; i++) {
87 for (
auto j = adj[i].begin(); j != adj[i].end(); j++) {
88 grev[*j].push_back(i);
94 for (
int i = 0; i < V; i++) vis[i] =
false;
99 if (vis[t] ==
false) {
118 std::vector<std::vector<int> > adj(a + 1);
119 for (
int i = 0; i < b; i++)
126 std::cout << kosaraju(a, adj) << std::endl;
std::vector< size_t > dfs(const std::vector< std::vector< size_t > > &graph, size_t start)
Explores the given vertex, exploring a vertex means traversing over all the vertices which are connec...