Cod sursa(job #3325535)

Utilizator ana.veronica13Ana Veronica Draghici ana.veronica13 Data 25 noiembrie 2025 18:21:04
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 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;
}