Pagini recente » Cod sursa (job #1414644) | Cod sursa (job #669682) | Cod sursa (job #91564) | Cod sursa (job #357214) | Cod sursa (job #2375444)
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
#define NMAX 100001
vector<vector<int>> graph = vector<vector<int>>(NMAX, vector<int>());
vector<vector<int>> biconex_components;
vector<int> low = vector<int>(NMAX);
vector<int> id = vector<int> (NMAX, -1);
stack<int> st;
ifstream fin("biconex.in");
ofstream fout("biconex.out");
int n, m;
int nr = 0;
int idx = 0;
void biconex(int node) {
id.at(node) = low.at(node) = ++idx;
st.push(node);
for(auto& elem : graph.at(node)) {
if(id.at(elem) != -1) {
low.at(node) = min(id.at(elem), low.at(node));
continue;
}
biconex(elem);
low.at(node) = min(low.at(elem), low.at(node));
if(low.at(elem) >= id.at(node)) {
nr++;
vector<int> biconex_component;
int bc_node;
do {
bc_node = st.top();
st.pop();
biconex_component.push_back(bc_node);
} while(bc_node != elem);
biconex_component.push_back(node);
biconex_components.push_back(biconex_component);
}
}
}
int main()
{
fin >> n >> m;
int x, y;
for(int i = 1; i <= m; i++) {
fin >> x >> y;
graph.at(x).push_back(y);
graph.at(y).push_back(x);
}
biconex(1);
fout << nr << "\n";
for(auto& bc : biconex_components) {
for(auto& node : bc)
fout << node << " ";
fout << "\n";
}
}