Cod sursa(job #3357865)

Utilizator TestLicenta123Test Test TestLicenta123 Data 13 iunie 2026 18:29:35
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

ifstream cin("sortaret.in");
ofstream cout("sortaret.out");

vector<vector<int>> gr(50100);
vector<int> top(50100);
queue<int> Q;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int nodes, edges;
    cin >> nodes >> edges;

    for (int i = 0; i < edges; i++) {
        int a, b;
        cin >> a >> b;
        gr[a].push_back(b);
        top[b]++;
    }

    for (int i = 1; i <= nodes; i++) {
        if (top[i] == 0) {
            Q.push(i);
        }
    }

    while (!Q.empty()) {
        int now = Q.front();
        Q.pop();
        cout << now << " ";
        for (auto &x : gr[now]) {
            top[x]--;
            if (top[x] == 0) {
                Q.push(x);
            }
        }
    }

    return 0;
}