Pagini recente » Cod sursa (job #284440) | Cod sursa (job #1678740) | Cod sursa (job #1682074) | Cod sursa (job #2941245) | Cod sursa (job #2172556)
#include <fstream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
using VI = vector<int>;
using VVI = vector<VI>;
using VB = vector<bool>;
int n, m, nv;
VVI G, ctc;
VI niv, L, c;
stack<int> stk;
VB inStack;
void Read();
void Df(int x);
void Write();
int main()
{
Read();
for (int x = 1; x <= n; x++)
if (!niv[x])
Df(x);
Write();
}
void Write()
{
ofstream fout("ctc.out");
fout << ctc.size() << '\n';
for (const VI& c : ctc)
{
for (const int& x : c)
fout << x << ' ';
fout << '\n';
}
fout.close();
}
void Df(int x)
{
niv[x] = L[x] = ++nv;
stk.push(x), inStack[x] = true;
for (const int& y : G[x])
if (!niv[y])
{
Df(y);
L[x] = min(L[x], L[y]);
}
else
if (inStack[y])
L[x] = min(L[x], niv[y]);
if (L[x] == niv[x])
{
c.clear();
while (!stk.empty())
{
int top = stk.top();
stk.pop();
inStack[top] = false;
c.push_back(top);
if (top == x)
break;
}
ctc.push_back(c);
}
}
void Read()
{
ifstream fin("ctc.in");
fin >> n >> m;
G = VVI(n + 1);
niv = L = VI(n + 1);
inStack = VB(n + 1);
int x, y;
while (m--)
{
fin >> x >> y;
G[x].push_back(y);
}
fin.close();
}