Cod sursa(job #1589912)

Utilizator AnesthesicChereches Sergiu Alexandru Anesthesic Data 4 februarie 2016 16:01:38
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include <iostream>
#include <vector>
#include <fstream>
#define nmax 50005
using namespace std;

void read_data (int &n, vector <int> v[nmax]){
    ifstream fin ("sortaret.in");
    int m;
    fin >> n >> m;
    for (int i = 1; i <= m; i++){
        int x, y;
        fin >> x >> y;
        v[x].push_back (y);
    }
}

void dfs (int x, int n, bool seen[nmax], vector <int> v[nmax], vector <int> &sol){
    seen[x] = true;
    sol.push_back (x);
    for (auto neigh : v[x])
        if (!seen[neigh])
            dfs (neigh, n, seen, v, sol);
}

void solve (bool seen[nmax], int n, vector <int> v[nmax]){
    ofstream fout ("sortaret.out");
    vector <int> sol;
    for (int i = 1; i <= n; i++)    seen[i] = 0;
    for (int i = 1; i <= n; i++)
        if (!seen[i])
            dfs (i, n, seen, v, sol);
    for (auto x : sol)
        fout << x << " ";
}

int main(){
    vector <int> v[nmax];
    int n;
    bool seen[nmax];
    read_data (n, v);
    solve (seen, n, v);
    return 0;
}