Cod sursa(job #1908708)

Utilizator vladm98Munteanu Vlad vladm98 Data 7 martie 2017 10:04:43
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <bits/stdc++.h>

using namespace std;
vector <int> graf[50001];
deque<int> coada;
int grad[50001];
vector <int> solutie;
int main()
{
    ifstream fin ("sortaret.in");
    ofstream fout ("sortaret.out");
    int n, m;
    fin >> n >> m;
    for (int i = 0; i<m; ++i)
    {
        int x, y;
        fin >> x >> y;
        grad[y]++;
        graf[x].push_back(y);
    }
    for (int i = 1; i<=n; ++i)
        if (grad[i]==0)
            coada.push_back(i);
    while (coada.empty()==0)
    {
        int nod = coada.front();
        solutie.push_back(nod);
        coada.pop_front();
        for (auto x:graf[nod])
        {
            grad[x]--;
            if (grad[x]==0)
                coada.push_back(x);
        }
    }
    for (auto x:solutie)
        fout << x << " ";
    return 0;
}