Pagini recente » Cod sursa (job #296675) | Cod sursa (job #512843) | Cod sursa (job #1382183) | Cod sursa (job #1373540) | Cod sursa (job #1573950)
#include <fstream>
#include <stack>
using namespace std;
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
stack <int> S;
struct nod
{ int info;
nod *urm;
};
nod *a[50001];
int n,m,x,y;
void Add(nod *prim , int y)
{ nod *q=new nod;
q->info=y;
q->urm=prim;
prim=q;
}
void Citire()
{ fin>>n>>m;
int i;
for(i=1;i<=m;i++)
{fin>>x>>y;
Add(a[x],y);
}
}
int viz[50001];
void DFS(int x)
{ viz[x]=1;//S.push(x);
for(nod *p=a[x];p!=NULL;p=p->urm)
if(viz[p->info]==0) DFS(p->info);
S.push(x);
}
void Timpi()
{ int i;
for(i=1;i<=n;i++)
if(viz[i]==0) DFS(i);
}
void Afisare()
{ int xx;
while(!S.empty())
{ xx=S.top();S.pop();
fout<<xx<<" ";
}
}
int main()
{ Citire();
Timpi();
Afisare();
return 0;
}