Cod sursa(job #2641886)

Utilizator BAlexandruBorgovan Alexandru BAlexandru Data 12 august 2020 23:39:24
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 f("sortaret.in");
ofstream g("sortaret.out");

int n, m;
int deg[50001];

vector < int > graph[50001], v;

queue < int > q;

int main()
{
    f >> n >> m;
    for (int i=1; i<=m; i++)
    {
        int x, y; f >> x >> y;
        graph[x].push_back(y);
        deg[y]++;
    }

    for (int i=1; i<=n; i++)
        if (!deg[i])
            q.push(i);

    while (!q.empty())
    {
        int node = q.front();
        q.pop();

        v.push_back(node);

        for (auto it : graph[node])
        {
            deg[it]--;
            if (!deg[it])
                q.push(it);
        }
    }

    for (auto it : v)
        g << it << " ";

    return 0;
}