Cod sursa(job #2872623)

Utilizator RaresCelescuRares Celescu RaresCelescu Data 17 martie 2022 16:12:16
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.84 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 in("sortaret.in");
    ofstream out("sortaret.out");
    int m, n;
    in >> n >> m;
    for (int i = 0; i < m; i++)
    {
        int x, y;
        in >> 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();
        q.pop();
        out << x << " ";
        for (auto y: a[x])
        {
            nrp[y]--;
            if (nrp[y] == 0)
            {
                q.push(y);
            }
        }
    }
    in.close();
    out.close();
    return 0;
}