Cod sursa(job #1401848)

Utilizator irimiecIrimie Catalin irimiec Data 26 martie 2015 10:15:12
Problema Sortare topologica Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 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     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 50010

int n, m;
vector<int> vec[MAXN];
bitset<MAXN> viz;
stack<int> sol;

void read() {
    #ifndef ONLINE_JUDGE
    freopen("D:/C++/in", "r", stdin);
    freopen("D:/C++/out", "w", stdout);
    #endif

    int x, y;
	scanf("%d%d", &n, &m);
	while(m--) {
        scanf("%d%d", &x, &y);
        vec[x].pub(y);
	}
}

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

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

    while(!sol.empty()) {
        int nod = sol.top(); sol.pop();
        cout << nod << " ";
    }

    return 0;
}