Pagini recente » Cod sursa (job #2208239) | Cod sursa (job #2167844) | Cod sursa (job #851846) | Cod sursa (job #188030) | Cod sursa (job #2079121)
#include <fstream>
#define NMAX 50005
using namespace std;
ifstream in("sortaret.in");
ofstream out("sortaret.out");
int n, m, viz[NMAX];
struct nod
{
int vf;
nod *next;
} *L[NMAX], *sol;
void add(int a, int b)
{
nod *p = new nod;
p->vf = b;
p->next = L[a];
L[a] = p;
}
void citire()
{
in >> n >> m;
for(int x, y; m; --m)
{
in >> x >> y;
add(x, y);
}
}
void build_sol(int k)
{
nod *p = new nod;
p->vf = k;
p->next = sol;
sol = p;
}
void DF(int k)
{
viz[k] = 1;
for(; L[k]; L[k] = L[k]->next)
if(!viz[L[k]->vf])
DF(L[k]->vf);
build_sol(k);
}
void sortare_topologica()
{
for(int i = 1; i <= n; ++i)
if(!viz[i])
DF(i);
}
void afisare()
{
while(sol)
{
out << sol->vf << ' ';
sol = sol->next;
}
}
int main()
{
citire();
sortare_topologica();
afisare();
return 0;
}