Cod sursa(job #2943065)

Utilizator vlad_dimuVlad Dimulescu vlad_dimu Data 20 noiembrie 2022 15:52:05
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <fstream>
#include <algorithm>
#include <vector>
#define MAXN 50000
using namespace std;

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

vector <int> graph[MAXN];
vector <int> topologic;
int marked[MAXN];

void dfs( int nod ){
  int i, vecin;
  marked[nod] = 1;

  for( i = 0; i < graph[nod].size(); i++ ){
    vecin = graph[nod][i];
    if( marked[vecin] == 0 )
      dfs( vecin );
  }

  topologic.push_back( nod );

}

int main(){
    int n, m, i, x, y;
    fin >> n >> m;
    for( i = 0; i < m; i++ ){
      fin >> x >> y;
      graph[x].push_back(y);
    }

    dfs(1);
    reverse( topologic.begin(), topologic.end() );

    for( i = 0; i < topologic.size(); i++ )
      fout << topologic[i] << ' ';

    return 0;
}