Cod sursa(job #1707073)

Utilizator Theodor1000Cristea Theodor Stefan Theodor1000 Data 24 mai 2016 09:50:32
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <cstdio>
#include <algorithm>
#include <vector>
#include <stack>

using namespace std;

vector <int> v[50010];
stack <int> st;
bool ap[50010];

inline void dfs (int nod)
{
    ap[nod] = true;

    for (auto &it : v[nod])
        if (!ap[it]) dfs (it);

    st.push (nod);
}

int main ()
{
    freopen ("sortaret.in", "r", stdin);
    freopen ("sortaret.out", "w", stdout);

    int n, m;
    scanf ("%d %d", &n, &m);

    for (int i = 1; i <= m; ++i)
    {
        int x, y;
        scanf ("%d %d", &x, &y);

        v[x].push_back (y);
    }

    for (int i = 1; i <= n; ++i)
        if (!ap[i]) dfs (i);

    while (!st.empty ())
    {
        int x = st.top ();
        st.pop ();

        printf ("%d ", x);
    }

    printf ("\n");

    return 0;
}