Cod sursa(job #2116937)

Utilizator tanasaradutanasaradu tanasaradu Data 28 ianuarie 2018 12:43:33
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.04 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("sortaret.in");
ofstream fout ("sortaret.out");
const int Mmax = 100001;
const int Nmax = 50005;
int n , m , g[Mmax];
vector < int > L[Nmax];
struct T
{
    int val;
    bool operator < (const T &e) const
    {
        return val > e . val;
    }
};
priority_queue < T > Q;
int main()
{
    int x , y;
    T w , w1;
    fin >> n >> m;
    while(m --)
    {
        fin >> x >> y;
        g[y]++;
        L[x] . push_back(y);
    }
    for(int i = 1 ; i <= n ; i++)
        if(g[i] == 0)
        {
            w . val = i;
            Q . push(w);
        }
    while(! Q . empty())
    {
        w = Q . top();
        fout << w . val << " ";
        Q . pop();
        for(auto i : L[w . val])
            if(g[i] > 0)
        {
            g[i]--;
            if(g[i] == 0)
            {
                w1 . val = i;
                Q . push(w1);
            }
        }
    }
    fout << "\n";

    fin.close();
    fout.close();
    return 0;
}