Pagini recente » Cod sursa (job #2409261) | Cod sursa (job #844264) | Cod sursa (job #824678) | Cod sursa (job #1706896) | Cod sursa (job #2298342)
#include <bits/stdc++.h>
#define MAXN 100005
int N, M;
std::vector <int> ADC[MAXN];
inline void AddEdge(int x, int y) {
ADC[x].push_back(y);
}
int LVL[MAXN], Low[MAXN];
std::stack <int> Stack;
int NBicnx;
std::vector <int> Bicnx[MAXN];
void DFS(int Vertex, int Parent = 0) {
LVL[Vertex] = Low[Vertex] = LVL[Parent] + 1;
Stack.push(Vertex);
for (auto Vecin:ADC[Vertex]) {
if (Vecin == Parent) continue;
if (!LVL[Vecin]) {
DFS(Vecin, Vertex);
Low[Vertex] = std::min(Low[Vertex], Low[Vecin]);
if (Low[Vecin] >= LVL[Vertex]) {
do {
Bicnx[NBicnx].push_back(Stack.top());
if (Stack.top() == Vecin) break;
Stack.pop();
} while (true);
Bicnx[NBicnx].push_back(Vertex);
Stack.pop();
++ NBicnx;
}
}
else Low[Vertex] = std::min(Low[Vertex], LVL[Vecin]);
}
}
std::ifstream In("biconex.in");
std::ofstream Out("biconex.out");
void Citire() {
In >> N >> M;
for (int i=0, x, y; i<M; ++i)
In >> x >> y, AddEdge(x, y);
}
void Rezolvare() {
for (int i=1; i<=N; ++i)
if (!LVL[i])
DFS(i);
Out << NBicnx << '\n';
for (int i=0, j; i<NBicnx; ++i, Out << '\n')
for (j=0; j<Bicnx[i].size(); ++j)
Out << Bicnx[i][j] << ' ' ;
}
int main()
{
Citire();
Rezolvare();
return 0;
}