Cod sursa(job #1207049)

Utilizator mariusn01Marius Nicoli mariusn01 Data 11 iulie 2014 22:12:00
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <fstream>
#include <vector>

#define DIM 50010


using namespace std;

vector<int> L[DIM];
int v[DIM];
int d[DIM];
int S[DIM];
int n, m, i, k, x, y;

using namespace std;

void dfs(int nod) {
    v[nod] = 1;
    for (int i=0;i<L[nod].size();i++)
        if (v[ L[nod][i] ] == 0)
            dfs( L[nod][i] );
    S[++k] = nod;
}

int main (){
    ifstream fin("sortaret.in");
    ofstream fout("sortaret.out");
    fin>>n>>m;
    for (i=1;i<=m;i++) {
        fin>>x>>y;
        L[x].push_back(y);
        d[y]++;
    }
    for (i=1;i<=n;i++)
        if (d[i] == 0)
            dfs(i);

    for (i=n;i>=1;i--)
        fout<<S[i]<<" ";

    return 0;
}