Cod sursa(job #2721010)

Utilizator codrut86Coculescu Ioan-Codrut codrut86 Data 11 martie 2021 14:52:20
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

ifstream in("sortaret.in");
ofstream out("sortaret.out");

const int N=50001;
vector <int> a[N];
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]++;
    }

    queue <int> q;
    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 i:a[x])
        {
            nrp[i]--;
            if(nrp[i]==0)
            {
                q.push(i);
            }
        }
    }

    return 0;
}