Cod sursa(job #1606432)

Utilizator zaharia_horiaZaharia Horia zaharia_horia Data 20 februarie 2016 11:44:32
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include <fstream>
#include <vector>
#include <queue>

#define NMax 50010

using namespace std;

ifstream f("sortaret.in");
ofstream g("sortaret.out");

int nodes, edges, degIn[NMax];
vector<int> G[NMax];
queue<int> QU;

void updateDeg(int node)
{
    for (vector<int>::iterator it = G[node].begin(); it != G[node].end(); it++) {
        degIn[*it]--;

        if (degIn[*it] == 0)
            QU.push(*it);
    }
}

int main()
{
    f >> nodes >> edges;

    int n1, n2;
    for (int i = 1; i <= edges; i++) {
        f >> n1 >> n2;

        G[n1].push_back(n2);
        degIn[n2]++;
    }

    for (int i = 1; i <= nodes; i++)
        if (degIn[i] == 0)
            QU.push(i);

    while (!QU.empty()) {
        int crtNode = QU.front();
        QU.pop();

        g << crtNode << " ";
        updateDeg(crtNode);
    }

}