Pagini recente » Cod sursa (job #1659979) | Cod sursa (job #1387995) | Cod sursa (job #2983906) | Cod sursa (job #2302002) | Cod sursa (job #2228560)
#include <fstream>
#include <vector>
#define NMAX 100000
using namespace std;
ifstream fi("ctc.in");
ofstream fo("ctc.out");
int n, m, nr, nrComp;
int VIZ[NMAX+5], post[NMAX+5];
vector <int> G[NMAX+5], GT[NMAX+5], SOL[NMAX+5];
void citire(void)
{
int x, y;
fi>>n>>m;
for(int i=1; i<=m; i++)
{
fi>>x>>y;
G[x].push_back(y);
GT[y].push_back(x);
}
}
void DFS_post(int nod)
{
int nods;
VIZ[nod]=1;
for(int i=0; i<GT[nod].size(); i++)
{
nods=GT[nod][i];
if(!VIZ[nods])
DFS_post(nods);
}
post[++nr]=nod;
}
void DFS_comp(int nod)
{
int nods;
SOL[nrComp].push_back(nod);
VIZ[nod]=0;
for(int i=0; i<G[nod].size(); i++)
{
nods=G[nod][i];
if(VIZ[nods])
DFS_comp(nods);
}
}
void afisare(void)
{
fo<<nrComp<<"\n";
for(int i=1; i<=nrComp; i++)
{
for(int j=0; j<SOL[i].size(); j++)
fo<<SOL[i][j]<<" ";
fo<<"\n";
}
}
int main()
{
citire();
for(int i=1; i<=n; i++)
if(!VIZ[i])
DFS_post(i);
for(int i=n; i>=1; i--)
if(VIZ[post[i]])
{
nrComp++;
DFS_comp(post[i]);
}
afisare();
fi.close();
fo.close();
return 0;
}