Cod sursa(job #2134220)

Utilizator flaviu_2001Craciun Ioan-Flaviu flaviu_2001 Data 17 februarie 2018 19:06:50
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <bits/stdc++.h>

using namespace std;

int n, m, p, post[50005];
vector<int> v[50005];
bool ok[50005];

void df(int x)
{
    ok[x] = 1;
    for (vector<int>::iterator it = v[x].begin(); it != v[x].end(); ++it)
        if(!ok[*it]){
            ok[*it] = 1;
            df(*it);
        }
    post[++p] = x;
}

int main()
{
    ifstream fin ("sortaret.in");
    ofstream fout ("sortaret.out");
    fin >> n >> m;
    while(m--){
        int x, y;
        fin >> x >> y;
        v[x].push_back(y);
    }
    for (int i = 1; i <= n; ++i)
        if(!ok[i])
            df(i);
    for (int i = p; i >= 1; --i)
        fout << post[i] << " ";
    return 0;
}