Cod sursa(job #2152307)

Utilizator calinfloreaCalin Florea calinflorea Data 5 martie 2018 13:43:57
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.62 kb
#include <bits/stdc++.h>
#define NMax 50006
using namespace std;ifstream fin("sortaret.in");ofstream fout("sortaret.out");vector <int> L[NMax];int n, m, K;int drum[NMax];bool viz[NMax];inline void Citire(){int i, x, y;fin >> n >> m;for(i = 1; i <= m; i++){fin >> x >> y;L[x].push_back(y);}}void DFS(int nod){int i, j;viz[nod] = 1;for(j = 0; j < L[nod].size(); j++){i = L[nod][j];if(!viz[i])DFS(i);}drum[++K] = nod;}inline void SortareTop(){int i;for(i = 1; i <= n; i++)if(!viz[i])DFS(i);}inline void Afisare(){int i;for(i = n; i >= 1; i--)fout << drum[i] << " ";fout << "\n";}int main(){Citire();SortareTop();Afisare();    return 0;}