Cod sursa(job #2501804)

Utilizator cristina-criCristina cristina-cri Data 30 noiembrie 2019 11:26:51
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#include <cstdio>
#include <vector>
#include <stack>

using namespace std;

vector <int> graph[50005];
stack <int> s;

int n, m, x, y;
bool viz[50005];

void filll(int x)
{
    for(auto &v:graph[x])
    {
        if(viz[v] == 0)
        {
            viz[v]=1;
            filll(v);
        }
    }
    s.push(x);
}
int main()
{
    freopen("sortaret.in", "r", stdin);
    freopen("sortaret.out", "w", stdout);
    
    scanf("%d %d", &n, &m);
    
    for(int i=1; i<=m; i++)
    {
        scanf("%d %d", &x, &y);
        graph[x].push_back(y);
    }
    for(int i=1; i<=n; i++)
        if(viz[i]==0)
        {
            filll(i);
            viz[i]=1;
        }
    while(!s.empty())
    {
        printf("%d ", s.top());
        s.pop();
    }
    return 0;
}