Cod sursa(job #1378995)

Utilizator viuscenkoViktor Iuscenko viuscenko Data 6 martie 2015 15:29:45
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 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};
vector<int> vec[MAXN], sol;
bitset<MAXN> viz;

void dfs(int nod) {
    if(!viz[nod]) {
        viz[nod] = 1;
        for(auto it: vec[nod]) {
            dfs(it);
        }
        sol.pub(nod);
    }
}

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

    for (int i = 1; i <= n; ++i) {
        if(!viz[i])
            dfs(i);
    }

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

    return 0;
}