Cod sursa(job #2197511)

Utilizator YouDontNeedMyNameJurcut Paul YouDontNeedMyName Data 22 aprilie 2018 13:53:18
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <fstream>
using namespace std;
ifstream cin("sortaret.in");
ofstream cout("sortaret.out");
struct nod{
    int val;
    nod* next;
};
nod *lista[100005],*sortat;
int n,m,v[100005];
void add(int x,int y)
{
    nod *e=new nod;
    e->val=x;
    e->next=lista[y];
    lista[y]=e;
}
void PUSH(int x)
{
    nod *e=new nod;
    e->val=x;
    e->next=sortat;
    sortat=e;
}
void DFS(int x)
{
    v[x]=1;
    nod *e=lista[x];
    while(e)
    {
        if(!v[e->val])
        {
            DFS(e->val);
        }
        e=e->next;
    }
    PUSH(x);
}
int main()
{
    cin >> n >> m;
    for(int i=1; i<=m; i++)
    {
        int x,y;
        cin >> x >> y;
        add(y,x);
    }
    for(int i=1; i<=n; i++)
    {
        if(!v[i])
            DFS(i);
    }
    while(sortat)
    {
        cout << sortat->val << ' ';
        sortat=sortat->next;
    }
    return 0;
}