Pagini recente » Cod sursa (job #3274232) | Cod sursa (job #595905) | Cod sursa (job #2514422) | Cod sursa (job #2194363) | Cod sursa (job #1969415)
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
int n, m, idx;
vector<vector<int>> G, CC;
vector<int> Index, L, C;
vector<bool> InStack;
stack<int> Stk;
void Read();
void Tarjan(int x);
void Write();
int main() {
Read();
for (int i = 1; i <= n; i++)
if (Index[i] == 0) Tarjan(i);
Write();
fin.close();
fout.close();
return 0;
}
void Tarjan(int x) {
idx++;
Index[x] = L[x] = idx;
Stk.push(x);
InStack[x] = true;
for (const auto & y : G[x])
if (Index[y] == 0) {
Tarjan(y);
L[x] = min(L[x], L[y]);
}
else if (InStack[y])
L[x] = min(L[x], Index[y]);
if (Index[x] == L[x]) {
C.clear();
int nod;
do {
nod = Stk.top();
Stk.pop();
InStack[nod] = false;
C.push_back(nod);
} while (nod != x);
CC.push_back(C);
}
}
void Write() {
fout << CC.size() << '\n';
for (const auto & c : CC) {
for (const auto & x : c)
fout << x << ' ';
fout << '\n';
}
}
void Read() {
fin >> n >> m;
G = vector<vector<int>>(n + 1);
Index = L = vector<int>(n + 1);
InStack = vector<bool>(n + 1);
int x, y;
for (int i = 1; i <= m; i++) {
fin >> x >> y;
G[x].push_back(y);
}
}