This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub kmyk/competitive-programming-library
#include "graph/connected_components.hpp"
#pragma once
#include <stack>
#include <vector>
#include "../utils/macros.hpp"
/**
* @param g is an undirected graph
*/
int count_connected_components(const std::vector<std::vector<int> > & g) {
int n = g.size();
int cnt = 0;
std::vector<bool> used(n);
std::stack<int> stk;
REP (z, n) if (not used[z]) {
++ cnt;
used[z] = true;
stk.push(z);
while (not stk.empty()) {
int x = stk.top();
stk.pop();
for (int y : g[x]) if (not used[y]) {
used[y] = true;
stk.push(y);
}
}
}
return cnt;
}
/**
* @param g is an undirected graph
*/
bool is_connected_graph(const std::vector<std::vector<int> > & g) {
return count_connected_components(g) == 1;
}
#line 2 "graph/connected_components.hpp"
#include <stack>
#include <vector>
#line 2 "utils/macros.hpp"
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i))
#define ALL(x) std::begin(x), std::end(x)
#line 5 "graph/connected_components.hpp"
/**
* @param g is an undirected graph
*/
int count_connected_components(const std::vector<std::vector<int> > & g) {
int n = g.size();
int cnt = 0;
std::vector<bool> used(n);
std::stack<int> stk;
REP (z, n) if (not used[z]) {
++ cnt;
used[z] = true;
stk.push(z);
while (not stk.empty()) {
int x = stk.top();
stk.pop();
for (int y : g[x]) if (not used[y]) {
used[y] = true;
stk.push(y);
}
}
}
return cnt;
}
/**
* @param g is an undirected graph
*/
bool is_connected_graph(const std::vector<std::vector<int> > & g) {
return count_connected_components(g) == 1;
}