Cod sursa(job #2671795)

Utilizator robertnanu_fmiNanu Robert-Ionut robertnanu_fmi Data 12 noiembrie 2020 18:12:55
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.64 kb
#include <bits/stdc++.h>
using namespace std;

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

const int nmax=5e4+3;

int viz[nmax], st[nmax], a, m, n, k, b;
vector <int> x[nmax];

void dfs(int nod)
{
    viz[nod] = 1;
    for(int i = 0; i < x[nod].size(); ++i)
    {
        int next = x[nod][i];
        if(!viz[next]) dfs(next);
    }
    st[++k] = nod;
}

int main()
{
    f >> n >> m;
    while(m--)
    {
        f >> a >> b;
        x[a].push_back(b);
    }
    for(int i = 1; i <= n; ++i)
        if(!viz[i])
            dfs(i);
    for(int i = n; i >= 1; --i)
        g<<st[i]<<' ';
    return 0;
}