Cod sursa(job #2628438)

Utilizator doyouhavethetimeStanculescu Gabriel doyouhavethetime Data 15 iunie 2020 23:44:47
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.54 kb
#include <bits/stdc++.h>
#define N 50000
using namespace std;

bitset <N+1> seen;
vector <int> G[N+1], ans;
int dfs (int x) {
    seen[x]=1;
    for (auto it: G[x])
        dfs(it);
    ans.push_back(x);
}

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

    int n, m;
    fin >> n >> m;

    int i, j;
    for (; m; m--) {
        fin >> i >> j;
        G[i].push_back(j);
    }

    dfs(1);
    for (auto it=ans.rbegin(); it!=ans.rend(); ++it)
        fout << *it << ' ';
    return 0;
}