Cod sursa(job #2892780)

Utilizator TeoRoGaming_YgVoinea Ionut-Florin TeoRoGaming_Yg Data 23 aprilie 2022 16:12:48
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

const int N = 50000;

vector <int> a[N+1];
int nrp[N+1];

int main()
{
    ifstream f("sortaret.in");
    ofstream g("sortaret.out");
    int m, n, i;
    f >> n >> m;
    for (i = 0; i < m; i++)
    {
        int x, y;
        f >> x >> y;
        a[x].push_back(y);
        nrp[y]++;
    }
    ///initializarea cozii
    queue <int> q;
    for (i = 1; i <= n; i++)
    {
        if (nrp[i] == 0)
        {
            q.push(i);
        }
    }
    while (!q.empty())
    {
        int x = q.front();
        q.pop();
        g << x << " ";
        for (auto y: a[x])
        {
            nrp[y]--;
            if (nrp[y] == 0)
            {
                q.push(y);
            }
        }
    }
    f.close();
    g.close();
    return 0;
}