Cod sursa(job #2638124)

Utilizator mex7Alexandru Valentin mex7 Data 27 iulie 2020 08:34:28
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <bits/stdc++.h>
#define ll long long
using namespace std;
 
ifstream fin("sortaret.in");
ofstream fout("sortaret.out");
 
bitset <50001> reached;
vector <int> adjacencyList[50001];
set <int> values;
 
void dfs(int current) {
    reached[current] = 1;
    cout << current << " ";
    for (int i = 0; i < adjacencyList[current].size(); i++)
        if (!reached[adjacencyList[current][i]])
            dfs(adjacencyList[current][i]);
}
 
int main() {
    int n, m, x, y;
 
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        cin >> x >> y;
        adjacencyList[x].push_back(y);
        values.insert(x);
        values.insert(y);
    }
 
    for (int it : values) 
        if (!reached[it])
            dfs(it);
    
    return 0;
}