Cod sursa(job #2560054)

Utilizator NotTheBatmanBruce Wayne NotTheBatman Data 27 februarie 2020 19:03:27
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.88 kb
#include <fstream>
#include <queue>

using namespace std;

const int N = 50005;

priority_queue <int, vector<int>, greater<int> > q;
vector <int> L[N];
int n, indeg[N];

void Read ()
{
    ifstream fin ("sortaret.in");
    int m;
    fin >> n >> m;
    while (m--)
    {
        int x, y;
        fin >> x >> y;
        L[x].push_back(y);
        indeg[y]++;
    }
    fin.close();
}

void Solve ()
{
    for (int i = 1; i <= n; i++)
        if (!indeg[i])
            q.push(i);
    ofstream fout ("sortaret.out");
    while (!q.empty())
    {
        int vertex = q.top();
        q.pop();
        fout << vertex << " ";
        for (auto next : L[vertex])
        {
            indeg[next]--;
            if (!indeg[next])
                q.push(next);
        }
    }
    fout.close();
}

int main()
{
    Read();
    Solve();
    return 0;
}