Cod sursa(job #1412050)

Utilizator AnesthesicChereches Sergiu Alexandru Anesthesic Data 1 aprilie 2015 08:36:44
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#define nmax 100005
using namespace std;

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

vector<int> v[nmax];
stack <int> s;
bool seen[nmax];
int n, m;

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

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