Cod sursa(job #2164996)

Utilizator RaduVFVintila Radu-Florian RaduVF Data 13 martie 2018 10:45:39
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <list>
using namespace std;

ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
const int nmax = 50005;
vector < int > Muchii[nmax];
list < int > sol;
int n, m, x, y;
bool viz[nmax];

void DFS (int Nod) {
    sol.push_back(Nod);
    viz[Nod]=true;
    for(unsigned i=0; i<Muchii[Nod].size(); i++) {
        int v = Muchii[Nod][i];
        if(!viz[v]) DFS(v);
    }
}
int main()
{
    fin >> n >> m;
    for(int i=1; i<=m; i++) {
        fin >> x >> y;
        Muchii[x].push_back(y);
    }
    DFS(1);
    while ( !sol.empty() ) {
        fout << sol.front() << ' ' ;
        sol.pop_front();
    }
    return 0;
}