Cod sursa(job #1355217)

Utilizator viuscenkoViktor Iuscenko viuscenko Data 22 februarie 2015 15:22:55
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.52 kb
#include <bits/stdc++.h>

using namespace std;

#define     mp              make_pair
#define     fs              first
#define     sc              second
#define     pob             pop_back
#define     pub             push_back
#define     puf             push_front
#define     pof             pop_front
#define     eps             1E-7
#define     sz(a)           a.size()
#define     count_one       __builtin_popcount;
#define     count_onell     __builtin_popcountll;
#define     fastIO          ios_base::sync_with_stdio(false)
#define     PI              (acos(-1.0))
#define     linf            (1LL<<62)//>4e18
#define     inf             (0x7f7f7f7f)//>2e9

#define MAXN 50001
#define DEBUG 1
#ifdef DEBUG
#define D(x) x
#else
#define D(x)
#endif

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

int n, m;
int x, y;
int deg[MAXN] = {0};
deque<int> S;
vector<int> vec[MAXN], sol;

int main()
{
    f >> n >> m;
    for (int k = 0; k < m; ++k) {
        f >> x >> y;
        vec[x].pub(y);
        deg[y]++;
    }

    int i = 1;
    while ((i <= n) && (deg[i] > 0))
        i++;

    S.pub(i);

    while (!S.empty()) {
        int nod = S.front(); S.pof();
        sol.pub(nod);
        for (vector<int>::iterator it = vec[nod].begin(); it != vec[nod].end(); it++) {
            deg[*it]--;
            if(deg[*it] == 0)
                S.pub(*it);
        }
    }

    for(auto it = sol.begin(); it != sol.end(); it++) {
        g << *it << " ";
    }

    return 0;
}