Pagini recente » Cod sursa (job #97404) | Cod sursa (job #14417) | Cod sursa (job #1012719) | Cod sursa (job #832943) | Cod sursa (job #1839213)
#include <fstream>
#define NM 100005
#include <vector>
using namespace std;
ifstream f("ctc.in");
ofstream g("ctc.out");
vector < int > GT[NM], Gsol[NM], G[NM];
int n, m, viz[NM], where[NM], postordine[NM], nr, sol;
void Read()
{
int x, y;
f >> n >> m;
for(int i = 1; i <= m; ++i)
{
f >> x >> y;
G[x].push_back(y);
GT[y].push_back(x);
}
}
void DFST(int x)
{
where[x] = sol;
for(int i = 0 ; i < GT[x].size(); ++i)
{
if(!where[ GT[x][i] ])
DFST( GT[x][i] );
}
}
void DFS(int x)
{
viz[x] = 1;
for(int i = 0 ; i < G[x].size(); ++i)
{
if(viz[G[x][i]] == 0) DFS(G[x][i]);
}
postordine[++nr] = x;
}
int main()
{
Read();
for(int i = 1; i <= n; ++i)
if(!viz[i]) DFS(i);
for(int i = n; i > 0; --i)
{
if( !where[ postordine[ i ] ] )
{
sol++;
DFST(postordine[i]);
}
}
for(int i = 1; i <= n; ++i)
Gsol[where[i]].push_back(i);
g << sol << '\n';
for(int i = 1; i <= sol; ++i)
{
for(int j = 0; j < Gsol[i].size(); ++j)
g << Gsol[i][j] << ' ';
g << '\n';
}
}