Cod sursa(job #2905970)

Utilizator andreeat27Andreea Taclit andreeat27 Data 24 mai 2022 17:55:13
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

vector <int> a[50001];
int nrp[50001], n, m;

int main()
{
    ifstream fin("sortaret.in");
    ofstream fout("sortaret.out");
    fin >> n >> m;
    for (int i = 0; i < m; i ++)
    {
        int x, y;
        fin >> x >> y;
        a[x].push_back(y);
        nrp[y]++;
    }

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

    while (!q.empty())
    {
        int x=q.front();
        fout<<x<<" ";
        q.pop();
        for (auto y:a[x])
        {
            nrp[y]--;
            if (nrp[y]==0)
                q.push(y);
        }
    }
    fin.close();
    fout.close();
    return 0;
}