Pagini recente » Cod sursa (job #2871307) | Cod sursa (job #1134016) | Cod sursa (job #2078358) | Cod sursa (job #1657386) | Cod sursa (job #2096514)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream fin("biconex.in");
ofstream fout("biconex.out");
const int NMAX = 100000 + 5;
const int INF = 0x3f3f3f3f;
vector <int> graph[NMAX], t[NMAX], comp[NMAX];
stack <int> s;
int n, m, top, nr;
int niv[NMAX], fat[NMAX], st[NMAX], nivmin[NMAX];
bool vis[NMAX];
void dfs(int node)
{
vis[node] = true;
niv[node] = niv[fat[node]] + 1;
for (int i: graph[node])
if (!vis[i])
{
fat[i] = node;
t[node].push_back(i);
s.push(i);
dfs(i);
if (nivmin[i] >= niv[node])
{
++nr;
while (s.top() != i)
{
comp[nr].push_back(s.top());
s.pop();
}
comp[nr].push_back(s.top());
s.pop();
}
}
int minim = INF;
for (int i: t[node]) minim = min(minim, nivmin[i]);
for (int i: graph[node]) minim = min(minim, niv[i]);
nivmin[node] = minim;
}
void write()
{
fout << nr << '\n';
for (int i = 1; i <= nr; ++i)
{
for (int j: comp[i])
fout << j << " ";
fout << fat[comp[i][comp[i].size() - 1]] << '\n';
}
}
void read()
{
int x, y;
fin >> n >> m;
for (int i = 1; i <= m; ++i)
{
fin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
}
int main()
{
read();
dfs(1);
write();
return 0;
}