Pagini recente » Borderou de evaluare (job #3366238) | Monitorul de evaluare | Cod sursa (job #3364765) | Cod sursa (job #3366238) | Cod sursa (job #3366237)
#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 ctc;
int n, m;
void AddComp(int nod)
{
VI comp;
while (!st.empty())
{
comp.push_back(st.top());
if (st.top() == nod)
break;
st.pop(); // nu mai sterg nodul de plecare; acesta poate sa faca parte din mai multe ctc
}
ctc.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);
}
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 << ctc.size() << '\n';
for (auto c : ctc)
{
for (auto x : c)
fout << x << " ";
fout << '\n';
}
return 0;
}