Cod sursa(job #3205537)

Utilizator vvvvvvvvvvvvvVusc David vvvvvvvvvvvvv Data 19 februarie 2024 22:11:44
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.59 kb
#include <fstream>
#include <vector>
#include <stack>

using namespace std;

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

const int NMAX = 50001;
vector<int> g[NMAX];
int v[NMAX], n, m;
stack<int> s;

void dfs(int k){
    v[k] = 1;
    for(auto vec : g[k]) if(!v[vec]) dfs(vec);
    s.push(k);
}

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