Cod sursa(job #2657787)

Utilizator DeliaGhergheGherghe Ioana-Delia DeliaGherghe Data 11 octombrie 2020 23:25:04
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;
vector <int> l[50002];
int N,M,g[50002];
queue <int> q;


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

    fin >> N >> M;
    int i,j,k,nod;
    for (i = 1; i <= M; i++)
    {
        fin >> j >> k;
        l[j].push_back(k);
        g[k]++;
    }

    for (i = 1; i <= N; i++)
        if(g[i] == 0)
        q.push(i);

    while (!q.empty())
    {
        nod = q.front();
        fout << nod << " ";
        q.pop();
        for (i = 0; i < l[nod].size(); i++)
            {
                g[l[nod][i]]--;
                if (g[l[nod][i]] == 0)
                    q.push(l[nod][i]);
            }
    }

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