Pagini recente » Cod sursa (job #3189284) | Clasament iiot45 | Cod sursa (job #113734) | Cod sursa (job #2214349) | Cod sursa (job #1250476)
#include <fstream>
#include <vector>
#define NMAX 10001
using namespace std;
ifstream fin ("ctc.in");
ofstream fout ("ctc.out");
int A[NMAX][NMAX], A_t[NMAX][NMAX];
int uz[NMAX], uz_t[NMAX], post[NMAX];
int n, m, lg, nrc;
void citire()
{
int x, y;
fin>>n>>m;
for (int i=1; i<=m; i++)
{
fin>>x>>y;
A[x][++A[x][0]]=y;
A_t[y][++A_t[y][0]]=x;
}
}
void DFS(int k)
{
uz[k]=1;
for (int i=1; i<=A[k][0]; i++)
if (!uz[A[k][i]])
DFS(A[k][i]);
post[++lg]=k;
}
void DFS_t(int k)
{
uz_t[k]=nrc;
for (int i=1; i<=A_t[k][0]; i++)
if (!uz_t[A_t[k][i]])
DFS_t(A_t[k][i]);
}
void afisare()
{
fout<<nrc<<'\n';
for (int i=1; i<=nrc; i++)
{
for (int j=1; j<=n; j++)
if(uz_t[j]==i)
fout<<j<<" ";
fout<<'\n';
}
}
int main()
{
citire();
for (int i=1; i<=n; i++)
if (!uz[i])
DFS(i);
for (int i=n; i>=1; i--)
if (!uz_t[post[i]])
{
nrc++;
DFS_t(post[i]);
}
afisare();
return 0;
}