Cod sursa(job #1165282)

Utilizator tudorv96Tudor Varan tudorv96 Data 2 aprilie 2014 16:38:14
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <fstream>
#include <queue>
using namespace std;

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

const int N = 5e4 + 5;

int n, m;
vector <int> g[N], sol;
queue <int> Q;
int in[N];

int main() {
    fin >> n;
    for (int x, y, i = 0; i < m; ++i) {
        fin >> x >> y;
        g[x].push_back (y);
        in[y]++;
    }
    for (int x = 1; x <= n; ++x)
        if (!in[x]) {
            sol.push_back (x);
            Q.push (x);
        }
    while (Q.size()) {
        int x = Q.front(); Q.pop();
        for (vector <int> :: iterator it = g[x].begin(); it != g[x].end(); ++it) {
            in[*it]--;
            if (!in[*it]) {
                Q.push (*it);
                sol.push_back (*it);
            }
        }
    }
    for (vector <int> :: iterator it = sol.begin(); it != sol.end(); ++it)
        fout << *it << " ";
}