Cod sursa(job #3215628)

Utilizator victor_gabrielVictor Tene victor_gabriel Data 15 martie 2024 11:03:25
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int DIM = 50010;

int n, m, x, y;
vector<int> l[DIM], nodes;
bool visited[DIM];

void dfs(int node) {
    visited[node] = true;
    for (auto adjNode : l[node])
        if (!visited[node])
            dfs(adjNode);
    nodes.push_back(node);
}

int main() {
    fin >> n >> m;
    for (int i = 1; i <= m; i++) {
        fin >> x >> y;
        l[x].push_back(y);
    }

    for (int node = 1; node <= n; node++)
        if (!visited[node])
            dfs(node);

    //reverse(nodes.begin(), nodes.end());
    for (auto node : nodes)
        fout << node << ' ';

    return 0;
}