Pagini recente » Cod sursa (job #3261132) | Cod sursa (job #3204867) | Cod sursa (job #3184497) | Cod sursa (job #3196042) | Cod sursa (job #3172159)
#include <bits/stdc++.h>
#define cin fin
#define cout fout
using namespace std;
ifstream fin("biconex.in");
ofstream fout("biconex.out");
int32_t main()
{
cin.tie(nullptr)->sync_with_stdio(false);
int n, m;
cin >> n >> m;
vector<vector<int>> g(n + 1);
for (int i = 1; i <= m; ++i)
{
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
stack<pair<int, int>> s;
vector<int> low(n + 1, INT_MAX);
vector<int> depth(n + 1);
vector<bool> viz(n + 1);
vector<vector<pair<int, int>>> ans;
function<void(int, int)> biconexe = [&](int node, int d)
{
depth[node] = d;
viz[node] = true;
for (auto i : g[node])
{
if (viz[i])
{
low[node] = min(low[node], depth[i]);
}
else
{
s.push({node, i});
biconexe(i, d + 1);
if (low[i] >= depth[node])
{
ans.push_back({});
while (!s.empty())
{
pair<int, int> qui = s.top();
ans.back().push_back(qui);
s.pop();
if (qui == pair<int, int>{node, i})
{
break;
}
}
}
low[node] = min(low[node], low[i]);
}
}
};
biconexe(1, 0);
cout << (int)ans.size() << '\n';
for (auto i : ans)
{
set<int> s;
for (auto j : i)
{
s.insert(j.first);
s.insert(j.second);
}
for (auto j : s)
{
cout << j << ' ';
}
cout << '\n';
}
}