Cod sursa(job #2469160)

Utilizator hunting_dogIrimia Alex hunting_dog Data 6 octombrie 2019 16:07:06
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <fstream>
#include <iostream>

using namespace std;

ifstream f("sortaret.in");
ofstream g("sortaret.out");

struct node
{
    int e;
    node *next=NULL;
}*v[50001],*res=NULL;

void push(node *(&e),int x)
{
    node *t=new node;
    t->e=x;
    t->next=e;
    e=t;
}

inline void sortare(int *viz,int i)
{
    viz[i]=1;
    if(!v[i])
        push(res,i);
    else
    {
        while(v[i])
        {
            if(!viz[v[i]->e])
                sortare(viz,v[i]->e);
            v[i]=v[i]->next;
        }
        push(res,i);
    }
}

int main()
{
    int n,m;
    f>>n>>m;
    for(int i=1;i<=n;++i)
        v[i]=NULL;
    while(m--)
    {
        int x,y;
        f>>x>>y;
        push(v[x],y);
    }
    int viz[50001]={0};
    for(int i=1;i<=n;++i)
        if(!viz[i])
            sortare(viz,i);
    while(res)
    {
        g<<res->e<<' ';
        res=res->next;
    }
    return 0;
}