Pagini recente » Monitorul de evaluare | Cod sursa (job #3364765) | Cod sursa (job #3366238) | Cod sursa (job #3366237) | Cod sursa (job #3365573)
#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;
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]);
}
}
int main()
{
int n, m;
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);
}
Dfs (1, 0);
fout << ctc.size() << '\n';
for (auto c : ctc)
{
for (auto x : c)
fout << x << " ";
fout << '\n';
}
return 0;
}