Cod sursa(job #2360624)

Utilizator Horea_Mihai_SilaghiHorea Mihai Silaghi Horea_Mihai_Silaghi Data 1 martie 2019 23:41:31
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.03 kb
#include <fstream>

using namespace std;
ifstream cin("sortaret.in");
ofstream cout("sortaret.out");
struct nod
{
    int vf;
    nod *next;
} *PNOD, NOD;

nod *L[50005];

nod *adresa;
int v[50005],n,m;
void add(int x, int y)
{
    nod *p=new nod;
    p->vf=y;
    p->next=L[x];
    L[x]=p;
}
void citire()
{
    int i,j,x,y;
    cin>>n>>m;
    for(i=1;i<=m;i++)
    {
        cin>>x>>y;
        add(x,y);
    }
}
void push(int x)
{
    nod *p=new nod;
    p->vf=x;
    p->next=adresa;
    adresa=p;
}
void df(int i)
{
    int j;
    v[i]=1;
    for(nod *it=L[i];it!=NULL;it=it->next)
    {
        if(v[it->vf]==0)
            df(it->vf);
    }
    v[i]=2;
    push(i);
}
void sortare_top()
{
    int i;
    for(i=1;i<=m;i++)
    {
        if(v[i]==0)
            df(i);
    }
}
void afis()
{
    nod *it=new nod;
    it=adresa;
    while(it)
    {
        cout<<it->vf<<" ";
        it=it->next;
    }
}
int main()
{
    citire();
    sortare_top();
    afis();
    return 0;
}