Pagini recente » Cod sursa (job #2827025) | Cod sursa (job #2114083) | Cod sursa (job #814596) | Cod sursa (job #714916) | Cod sursa (job #965151)
Cod sursa(job #965151)
#include <iostream>
#include <fstream>
#include <vector>
#define N 100005
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
int n, m, postord[N], nr, c;
vector <int> a[N], b[N], sol[N];
vector <bool> viz(N);
void dfs(int x)
{
viz[x] = 1;
for(unsigned i=0; i<a[x].size(); i++)
if(!viz[a[x][i]]) dfs(a[x][i]);
postord[++nr] = x;
}
void dfst(int x)
{
viz[x] = 0;
sol[c].push_back(x);
for(unsigned i=0; i<b[x].size(); i++)
if(viz[b[x][i]]) dfst(b[x][i]);
}
int main()
{
fin>>n>>m;
while(m--)
{
int x, y;
fin>>x>>y;
a[x].push_back(y);
b[y].push_back(x);
}
for(int i=1; i<=n; i++)
if(!viz[i]) dfs(i);
for(int i=n; i>0; i--)
if(viz[postord[i]])
{
c++;
dfst(postord[i]);
}
fout<<c<<'\n';
for(int i=1; i<=c; i++)
{
for(unsigned j=0; j<sol[i].size(); j++)
fout<<sol[i][j]<<' ';
fout<<'\n';
}
return 0;
}