Cod sursa(job #2101245)

Utilizator tanasaradutanasaradu tanasaradu Data 7 ianuarie 2018 00:20:50
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("sortaret.in");
ofstream fout ("sortaret.out");
const int Mmax = 100001;
const int Nmax = 50001;
int gr[Mmax] , n , m;
priority_queue < int > Q;
vector < int > L[Nmax];
int main()
{
    fin >> n >> m;
    for(int i = 1 ; i <= m ; i++)
    {
        int x , y;
        fin >> x >> y;
        L[x] . push_back(y);
        gr[y]++;
    }
    for(int i = 1 ; i <= n ; i++)
        if(gr[i] == 0)
            Q.push(- i);
    while(! Q . empty())
    {
        int x =  - Q . top();
        fout << x << " ";
        Q . pop();
        for(auto i : L[x])
        {
            gr[i]--;
            if(gr[i] == 0)
                Q.push( - i);
        }
    }
    fout << "\n";
    fin.close();
    fout.close();
    return 0;
}