Cod sursa(job #2666295)

Utilizator MValentinMunteanu Valentin-Ioan MValentin Data 1 noiembrie 2020 13:34:46
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <fstream>
#include <bits/stdc++.h>

using namespace std;

ifstream fin("sortaret.in");
ofstream fout("sortaret.out");

vector < vector <int> > graf;
queue <int> lista, sol;

int main()
{
    int n, m, x, y, i;
    fin >> n >> m;
    graf = vector < vector <int> > (n + 1);
    int grad_intern[100001];
    for(i = 0; i < m; i++)
    {
        fin >> x >> y;
        graf[x].push_back(y);
        grad_intern[y]++;
    }
    for(i = 1; i <= n; i++)
        if(grad_intern[i] == 0)
            lista.push(i);
    while(!lista.empty())
    {
        int nod_curent = lista.front();
        sol.push(nod_curent);
        lista.pop();
        int n_nod = graf[nod_curent].size();
        for(i = 0; i < n_nod; i++)
        {
            grad_intern[graf[nod_curent][i]]--;
            if(grad_intern[graf[nod_curent][i]] == 0)
                lista.push(graf[nod_curent][i]);
        }
    }
    while(!sol.empty())
    {
        fout << sol.front() << " ";
        sol.pop();
    }
}