Pagini recente » Cod sursa (job #858182) | Cod sursa (job #2057721) | Cod sursa (job #2340827) | Cod sursa (job #1341339) | Cod sursa (job #2372073)
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
using VI = vector<int>;
using VVI = vector<VI>;
using PI = pair<int, int>;
int n, m, nivel;
VI c, t, niv, L;
VVI G, comp;
stack<PI> stk;
void Read();
void Tarjan(int x);
void Write();
int main()
{
Read();
Tarjan(1);
Write();
}
void Write()
{
ofstream fout("biconex.out");
fout << comp.size() << '\n';
for (VI& c : comp)
{
sort(c.begin(), c.end());
c.erase(unique(c.begin(), c.end()), c.end());
for (const int& x : c)
fout << x << ' ';
fout << '\n';
}
fout.close();
}
void Tarjan(int x)
{
niv[x] = L[x] = ++nivel;
int x1, x2;
for (const int& y : G[x])
{
if (y == t[x])
continue;
if (!niv[y])
{
stk.push({x, y});
t[y] = x;
Tarjan(y);
L[x] = min(L[x], L[y]);
if (L[y] >= niv[x])
{
c.clear();
while (true)
{
x1 = stk.top().first;
x2 = stk.top().second;
stk.pop();
c.push_back(x1);
c.push_back(x2);
if (x1 == x && x2 == y)
break;
}
comp.push_back(c);
}
}
else
L[x] = min(L[x], niv[y]);
}
}
void Read()
{
ifstream fin("biconex.in");
fin >> n >> m;
G = VVI(n + 1);
niv = L = t = VI(n + 1);
int x, y;
for (int i = 0; i < m; i++)
{
fin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
fin.close();
}