Cod sursa(job #3343520)

Utilizator AndutsuAlexe Andrei-Cristian Andutsu Data 27 februarie 2026 17:42:44
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>
#include <queue>
#include <vector>
using namespace std;

const int N = 5e4;

vector <int> ls[N + 1];
int nr_pred[N + 1];

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

    int n, m;
    in >> n >> m;

    for (int i = 0; i < m; i++) {
        int x, y;
        in >> x >> y;
        ls[x].push_back(y);
        nr_pred[y]++;
    }
    in.close();

    queue <int> q;
    for (int i = 1; i <= n; i++) {
        if (nr_pred[i] == 0) {
            q.push(i);
        }
    }

    while (!q.empty()) {
        int x = q.front();
        q.pop();
        out << x << " ";
        for (auto y : ls[x]) {
            nr_pred[y]--;
            if (nr_pred[y] == 0) {
                q.push(y);
            }
        }
    }

    out.close();
    return 0;
}