Pagini recente » Cod sursa (job #1141581) | Cod sursa (job #1124231) | Cod sursa (job #300910) | pentru_adia | Cod sursa (job #3200132)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("biconex.in");
ofstream fout("biconex.out");
const int maxN = 100005;
int n, m;
int depth[maxN], lowlink[maxN];
bool used[maxN];
vector <int> stiva, comp, G[maxN];
vector <vector <int>> bicomp;
void dfs(int nod, int tata)
{
depth[nod] = depth[tata] + 1;
lowlink[nod] = depth[nod];
used[nod] = 1;
stiva.push_back(nod);
for(int vecin : G[nod])
{
if(vecin == tata)
continue;
if(used[vecin])
lowlink[nod] = min(lowlink[nod], depth[vecin]);
else
{
dfs(vecin, nod);
lowlink[nod] = min(lowlink[nod], lowlink[vecin]);
if(depth[nod] <= lowlink[vecin])
{
while(!stiva.empty() && stiva.back() != vecin)
{
comp.push_back(stiva.back());
stiva.pop_back();
}
stiva.pop_back();
comp.push_back(vecin);
comp.push_back(nod);
bicomp.push_back(comp);
comp.clear();
}
}
}
}
int main()
{
fin >> n >> m;
for(int i = 1; i <= m; i++)
{
int x, y;
fin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
for(int i = 1; i <= n; i++)
if(!used[i])
dfs(i, 0);
fout << bicomp.size() << '\n';
for(auto c : bicomp)
{
for(int x : c)
fout << x << ' ';
fout << '\n';
}
return 0;
}