Pagini recente » Cod sursa (job #2133183) | Cod sursa (job #1699698) | Cod sursa (job #1748784) | Cod sursa (job #1933874) | Cod sursa (job #1567606)
#include <cstdio>
#include <stack>
using namespace std;
//ifstream fin("ctc.in");
//ofstream fout("ctc.out");
FILE *f=fopen("ctc.in","r");
FILE *gg=fopen("ctc.out","w");
struct nod
{ int info;
nod*urm;
};
nod* g[100001]; bool viz[100001];
nod* gt[100001]; bool vizt[100001];
stack<int> S;
stack<int> ST;
int n,m;
inline void Add(nod *&prim,int x)
{ nod* p=new nod;
p->info=x;
p->urm=prim;
prim=p;
}
void Citire()
{ int i,x,y;
//fin>>n>>m;
fscanf(f,"%d%d",&n,&m);
for(i=1;i<=m;i++)
{ //fin>>x>>y;
fscanf(f,"%d%d",&x,&y);
Add(g[x],y);
Add(gt[y],x);
}
}
void DFS(int x)
{ viz[x]=true;
for(nod* p=g[x];p!=NULL;p=p->urm)
if(viz[p->info]==false)
DFS(p->info);
S.push(x);
}
void DFST(int x)
{ vizt[x]=true;
for(nod* p=gt[x];p!=NULL;p=p->urm)
if(vizt[p->info]==false)
DFST(p->info);
ST.push(x);
}
int main()
{ Citire();
int i,nr=0,x;
for(i=1;i<=n;i++)
if(viz[i]==false) DFS(i);
while(!S.empty())
{ x=S.top(); S.pop();
if(vizt[x]==false)
{ST.push(-1);DFST(x); nr++;}
}
//fout<<nr<<"\n";
fprintf(gg,"%d\n",nr);
while (!ST.empty())
{ x=ST.top();
if(x==-1) fprintf(gg,"\n");
else fprintf(gg,"%d ",x);
ST.pop();
}
fprintf(gg,"%f\n",(sizeof(g)*2+sizeof(viz)*2)/1024.0);
return 0;
}