Cod sursa(job #2723803)

Utilizator rarestudurTudur Rares rarestudur Data 15 martie 2021 17:09:09
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

const int N = 5e5;
int d[N],nrp[N];

vector <int> a[N];
queue <int> q;

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

void bfs()
{
    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);
            }
        }
    }
}

int main()
{
    int n,m;
    in >> n >> m;
    for(int i = 1; i <= m; i++)
    {
        int x,y;
        in >> x >>y;
        a[x].push_back(y);
        nrp[y]++;
    }
    for(int i=1; i <= n; i++)
    {
        if(nrp[i] == 0)
        {
            q.push(i);
        }
    }
    bfs();
    in.close();
    out.close();
    return 0;
}