Cod sursa(job #1807447)

Utilizator serbanSlincu Serban serban Data 16 noiembrie 2016 16:50:16
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <fstream>
#include <vector>

using namespace std;

vector<int> L[50005];
int c[50005];
bool viz[50005];

vector<int> v;

ofstream g("sortaret.out");

void df(int x) {
    viz[x] = true;
    for(int j = 0; j < L[x].size(); j ++) {
        if(!viz[L[x][j]]) df(L[x][j]);
    }
    v.push_back(x);
}

int main()
{
    ifstream f("sortaret.in");

    int n, m, x, y;
    f >> n >> m;
    for(int i = 1; i <= m; i ++) {
        f >> x >> y;
        L[x].push_back(y);
        c[y] ++;
    }

    for(int i = 1; i <= n; i ++) {
        if(!c[i])
            df(i);
    }
    for(int i = v.size() - 1; i >= 0; i --) g << v[i] << " ";
    g << "\n";
    return 0;
}