Cod sursa(job #1306479)

Utilizator AnesthesicChereches Sergiu Alexandru Anesthesic Data 31 decembrie 2014 01:01:43
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
#define nmax 100005

ifstream fin ("sortaret.in");
ofstream fout ("sortaret.out");

int n, m ,i, x, y;
vector <int> v[nmax];
vector <int> topp;
bool seen[nmax];

void dfs(int x){
    seen[x] = true;
    for(int i=0; i<v[x].size(); i++)
        if(!seen[v[x][i]]){
            seen[v[x][i]] = true;
            dfs(v[x][i]);
        }
        topp.push_back(x);
}

int main(){
    fin >> n >> m;
    for(i=1; i<=m; i++){
        fin >> x >> y;
        v[x].push_back(y);
    }
    for(i=1; i<=n; i++) if(!seen[i])  dfs(i);
    for(i=n-1; i>=0; i--)   fout << topp[i] << " ";
    return 0;
}