Pagini recente » Cod sursa (job #3364728) | Cod sursa (job #3364729) | Cod sursa (job #3366236) | Cod sursa (job #3364764) | Cod sursa (job #3366240)
#include <vector>
#include <fstream>
#include <stack>
using namespace std;
using VI = vector<int>;
using VVI = vector<VI>;
ifstream fin("biconex.in");
ofstream fout("biconex.out");
VVI adj;
VI index, low;
int idx;
stack<int> st;
VVI cbc;
int n, m;
void AddComp(int nod, int son)
{
VI comp;
int y;
do
{
y = st.top();
comp.push_back(y);
st.pop(); // nu mai sterg nodul de plecare; acesta poate sa faca parte din mai multe ctc
} while (y != son);
comp.push_back(nod);
cbc.push_back(comp);
}
void Dfs(int x, int fa)
{
index[x] = low[x] = ++idx;
st.push(x);
for (auto y : adj[x])
{
if (y == fa)
continue;
if (!index[y])
{
Dfs(y, x);
low[x] = min(low[x], low[y]);
if (low[y] >= index[x])
AddComp(x, y);
}
else
low[x] = min(low[x], index[y]);
}
}
void CBC()
{
for (int x = 1; x <= n; ++x)
if (!index[x])
{
Dfs(x, 0);
st = stack<int>();
}
}
int main()
{
fin >> n >> m;
int u, v;
adj = VVI(n + 1);
index = low = VI(n + 1);
for (int i = 1; i <= m; ++i)
{
fin >> u >> v;
adj[v].push_back(u);
adj[u].push_back(v);
}
CBC();
fout << cbc.size() << '\n';
for (auto c : cbc)
{
for (auto x : c)
fout << x << " ";
fout << '\n';
}
return 0;
}