Pagini recente » Cod sursa (job #1700857) | Cod sursa (job #1659518) | Cod sursa (job #1050828) | Cod sursa (job #2940564) | Cod sursa (job #2870664)
#include <bits/stdc++.h>
#define NMAX 100004
using namespace std;
ifstream fin ("ctc.in");
ofstream fout ("ctc.out");
int n, m, ctc;
bool viz[NMAX];
vector <int> G[NMAX], GT[NMAX], sol[NMAX], post_ordine;
void citire();
void DFS(int nod);
void DFST(int nod);
void afisare();
int main()
{
citire();
for (int i = 1; i <= n; i++)
if (!viz[i])
DFS(i);
for (int i = post_ordine.size() - 1; i >= 0; i--)
{
int val = post_ordine[i];
if (viz[val])
{
ctc++;
DFST(val);
}
}
afisare();
return 0;
}
void citire()
{
int x, y;
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
fin >> x >> y;
G[x].push_back(y);
GT[y].push_back(x);
}
}
void DFS(int nod)
{
viz[nod] = 1;
for (int i = 0; i < G[nod].size(); i++)
{
int vf = G[nod][i];
if (!viz[vf])
DFS(vf);
}
post_ordine.push_back(nod);
}
void DFST(int nod)
{
viz[nod] = 0;
sol[ctc].push_back(nod);
for (int i = 0; i < GT[nod].size(); i++)
{
int vf = GT[nod][i];
if (viz[vf])
DFST(vf);
}
}
void afisare()
{
fout << ctc << '\n';
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < sol[i].size(); j++)
fout << sol[i][j] << ' ';
fout << '\n';
}
}