Pagini recente » Cod sursa (job #1123698) | Istoria paginii utilizator/colossal_fuckup | Cod sursa (job #610948) | Cod sursa (job #632355) | Cod sursa (job #1896648)
#include <fstream>
#include <stack>
#include <vector>
using namespace std;
struct Node
{
int time;
int lowpoint;
vector<int> edges;
};
using Graph = vector<Node>;
void Dfs(Graph &g, int node, stack<int> &st, vector<vector<int>> &comps, int father = -1)
{
static int time = 0;
g[node].time = g[node].lowpoint = ++time;
st.push(node);
for (int next : g[node].edges) {
if (next == father) {
continue;
}
if (g[next].time == 0) {
Dfs(g, next, st, comps, node);
g[node].lowpoint = min(g[node].lowpoint, g[next].lowpoint);
if (g[next].lowpoint >= g[node].time) {
vector<int> new_comp;
while (st.top() != node) {
new_comp.push_back(st.top());
st.pop();
}
new_comp.push_back(node);
comps.push_back(new_comp);
}
} else {
g[node].lowpoint = min(g[node].lowpoint, g[next].time);
}
}
}
vector<vector<int>> FindComponents(Graph &g)
{
for (auto &node : g) {
node.time = node.lowpoint = 0;
}
vector<vector<int>> components;
for (unsigned i = 0; i < g.size(); ++i) {
if (g[i].time == 0) {
stack<int> st;
Dfs(g, i, st, components);
}
}
return components;
}
int main()
{
ifstream fin("biconex.in");
ofstream fout("biconex.out");
int n, m;
fin >> n >> m;
Graph graph(n);
while (m--) {
int x, y;
fin >> x >> y;
graph[x - 1].edges.push_back(y - 1);
graph[y - 1].edges.push_back(x - 1);
}
auto components = FindComponents(graph);
fout << components.size() << "\n";
for (const auto &comp : components) {
for (int node : comp) {
fout << node + 1 << " ";
}
fout << "\n";
}
return 0;
}