Cod sursa(job #2718925)

Utilizator elena_dElena Dulgheru elena_d Data 9 martie 2021 13:00:45
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <fstream>
#include <vector>
#include <queue>

ifstream in("sortaret.in");
ofstream out("sortaret.out");
using namespace std;
const int N = 50001;
vector <int> a[N];
queue <int> q;
int n, m, nrp[N];

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