Pagini recente » Cod sursa (job #2579183) | Cod sursa (job #786615) | Cod sursa (job #2347366) | Cod sursa (job #2229985) | Cod sursa (job #834609)
Cod sursa(job #834609)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("ctc.in");
ofstream fout("ctc.out");
#define NMAX 100005
vector <int> v[NMAX],t[NMAX],sol[NMAX];
int s[NMAX],k,z[NMAX];
bool viz[NMAX],scos[NMAX];
int n,m,rez,poz=0;
void dfs_init(int nod)
{
viz[nod]=true;
for(int i=0;i<v[nod].size();i++)
{
if(viz[v[nod][i]] == false)
{
dfs_init(v[nod][i]);
}
}
z[++poz]=nod;
}
void dfs(int nod)
{
if(s[nod]==0) sol[k].push_back(nod);
s[nod]=k;
for(int i=0;i<t[nod].size();i++)
if(s[t[nod][i]] != k)
dfs(t[nod][i]);
}
void read()
{
int i,a,b;
fin>>n>>m;
for(i=0;i<m;i++)
{
fin>>a>>b;
v[a].push_back(b);
t[b].push_back(a);
}
}
void solve()
{
int i,aux;
for(i=poz;i;i--)
{
if(s[z[i]]==0)
{
k++;
dfs(z[i]);
}
}
}
void tipar()
{
rez=k;
fout<<rez<<"\n";
for(int i=1;i<=rez;i++)
{
for(int j=0;j<sol[i].size();j++)
fout<<sol[i][j]<<" ";
fout<<"\n";
}
}
int main()
{
read();
dfs_init(1);
solve();
tipar();
}