Cod sursa(job #2754822)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 26 mai 2021 16:16:13
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.64 kb
#include <bits/stdc++.h>
 
using namespace std;
 
ifstream f("sortaret.in");
ofstream g("sortaret.out");
 
const int nmax = 50005;
vector<int> ls[nmax];
int viz[nmax], x, y, n, m, stiva[nmax], vf, i;
 
void dfs(int x) {
    int l = ls[x].size(), i, y;
    viz[x] = 1;
    for (i = 0; i < l; i++) {
        y = ls[x][i];
        if (viz[y]) continue;
        dfs(y);
    }
    stiva[++vf] = x;
}
 
int main() {
    f >> n >> m;
    while (m--) {
        f >> x >> y;
        ls[x].push_back(y);
    }
    for (i = 1; i <= n; i++)
        if (viz[i] == 0)
            dfs(i);
    while (vf)
        g << stiva[vf--] << ' ';
}