Cod sursa(job #1969805)

Utilizator tudormaximTudor Maxim tudormaxim Data 18 aprilie 2017 17:37:09
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;

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

const int maxn = 5e4 + 5;
vector <int> G[maxn];
bitset <maxn> Vis;
int Ans[maxn];
int n, m, lg;

void Dfs(int node) {
    Vis[node] = true;
    for (auto &it : G[node]) {
        if (Vis[it] == false) Dfs(it);
    }
    Ans[++lg] = node;
}

int main() {
    ios_base :: sync_with_stdio(false);
    fin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int x, y;
        fin >> x >> y;
        G[x].push_back(y);
    }
    Dfs(1);
    for (int i = lg; i > 0; i--) {
        fout << Ans[i] << " ";
    }
    fout << "\n";
    return 0;
}