Cod sursa(job #3364629)

Utilizator slol003Rizea Alexandru-Gabriel slol003 Data 8 septembrie 2026 09:10:26
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#include <bits/stdc++.h>

using namespace std;
const int NMAX = 5e4 + 5;
const int MMAX = 1e5 + 5;
queue<int> q;
vector<int> mat[NMAX];
int siz[NMAX];

void bfs(){
    while (!q.empty()){
        int x = q.front();
        q.pop();
        cout << x << ' ';
        for (auto i : mat[x]){
            siz[i] --;
            if (siz[i] == 0){
                q.push(i);
            }
        }
    }
}

int main(){
    int n, m;
    cin >> n >> m;
    for (int i = 1, x, y; i <= m; ++ i){
        cin >> x >> y;
        mat[x].push_back(y);
        siz[y] ++;
    }
    for (int i = 1; i <= n; ++ i){
        if (siz[i] == 0){
            q.push(i);
        }
    }
    bfs();

    return 0;
}