Pagini recente » Cod sursa (job #1998718) | Cod sursa (job #1100035) | Cod sursa (job #3190271) | Cod sursa (job #2205340) | Cod sursa (job #2954600)
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
const int nmax = 1e5 + 9;
vector<int> adj[nmax];
int lvl[nmax];
bool viz[nmax];
stack<int> st;
vector<vector<int>> bcc;
int dfs (int nod = 1)
{
viz[nod] = 1;
int reach = 1e9;
st.push(nod);
for (auto to : adj[nod])
{
if (viz[to])
{
reach = min(lvl[to] , reach);
continue;
}
lvl[to] = lvl[nod]+1;
int check = dfs(to);
reach = min(reach , check);
if (check >= lvl[nod])
{
bcc.push_back({nod});
while (!st.empty())
{
bcc.back().push_back(st.top());
st.pop();
if (bcc.back().back() == to) break;
}
}
}
return reach;
}
int main()
{
freopen("biconex.in" , "r" , stdin);
freopen("biconex.out" , "w" , stdout);
int n, m; cin >> n >> m;
for (int i = 1 ; i <= m ; i++)
{
int x, y; cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs();
cout << bcc.size() << '\n';
for (auto comp : bcc)
{
for (auto nod : comp) cout << nod << ' ';
cout << '\n';
}
}