Pagini recente » Cod sursa (job #1928851) | Cod sursa (job #1264385) | Cod sursa (job #2118514) | Cod sursa (job #1848698) | Cod sursa (job #2402656)
#include <stack>
#include <vector>
#include <fstream>
using std::stack;
using std::vector;
std::ifstream fin("biconex.in");
std::ofstream fout("biconex.out");
class Graph {
private:
int n;
vector<vector<int>> ad;
static inline int min(int x, int y) {
return x < y ? x : y;
}
void dfs(int node, int depth, vector<int>& lvl, vector<int>& low, stack<int>& st, vector<vector<int>>& bcc) {
low[node] = lvl[node] = depth;
for (int nghb : ad[node])
if (!lvl[nghb]) {
st.push(nghb);
dfs(nghb, depth + 1, lvl, low, st, bcc);
low[node] = min(low[node], low[nghb]);
if (low[nghb] >= lvl[node]) {
bcc.push_back(vector<int>(1, node));
while (st.top() != nghb) {
bcc.back().push_back(st.top());
st.pop();
}
bcc.back().push_back(st.top());
st.pop();
}
}
else if (lvl[nghb] < lvl[node] - 1)
low[node] = min(low[node], low[nghb]);
}
public:
Graph(int n) {
this->n = n;
ad.resize(n + 1);
}
void addEdge(int x, int y) {
ad[x].push_back(y);
ad[y].push_back(x);
}
void biconnectivity() {
stack<int> st;
vector<int> lvl(n + 1), low(n + 1);
vector<vector<int>> bcc;
dfs(1, 1, lvl, low, st, bcc);
fout << bcc.size() << '\n';
for (vector<int> comp : bcc) {
for (int node : comp)
fout << node << ' ';
fout << '\n';
}
}
};
int main() {
int n, m;
fin >> n >> m;
Graph graph(n);
for (int i = 0; i < m; i++) {
int x, y; fin >> x >> y;
graph.addEdge(x, y);
}
graph.biconnectivity();
fout.close();
return 0;
}