Pagini recente » Cod sursa (job #2949570) | Cod sursa (job #283520) | Cod sursa (job #1122529) | Cod sursa (job #531563) | Cod sursa (job #1896718)
#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)
{
static int time = 0;
g[node].time = g[node].lowpoint = ++time;
st.push(node);
for (int next : g[node].edges) {
if (g[next].time == 0) {
st.push(node);
Dfs(g, next, st, comps);
g[node].lowpoint = min(g[node].lowpoint, g[next].lowpoint);
if (g[next].lowpoint >= g[node].time) {
vector<int> new_comp;
while (new_comp.empty() || new_comp.back() != node) {
if (new_comp.empty() || new_comp.back() != st.top()) {
new_comp.push_back(st.top());
}
st.pop();
}
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;
}