Cod sursa(job #3364638)

Utilizator Lapush05alexandru Lapush05 Data 8 septembrie 2026 09:36:47
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <bits/stdc++.h>
using namespace std;

const int NMAX = 1000000;
vector<int> L[NMAX + 1];
int grad[NMAX + 1];

int main()
{
    int n, m, x, y;
    cin >> n >> m;
    for(int i=1; i<=m; i++){
        cin >> x >> y;
        L[x].push_back(y);
        grad[y]++;
    }
    queue<int>q;
    for(int i=1; i<=n; i++){
        if(grad[i]==0){
            q.push(i);
        }
    }
    while (q.empty()==false){
        int nod = q.front();
        q.pop();
        cout << nod <<" ";
        for(int i=0;i<L[nod].size(); i++){
            int vec=L[nod][i];
            grad[vec]--;
            if(grad[vec]==0){
                q.push(vec);
            }
        }
    }
    return 0;
}