Cod sursa(job #2084279)

Utilizator Teodor.mTeodor Marchitan Teodor.m Data 8 decembrie 2017 21:26:02
Problema Sortare topologica Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;

inline void debugMode() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("sortaret.in", "r", stdin);
    freopen("sortaret.out", "w", stdout);
    #endif // ONLINE_JUDGE
}
inline void PrintYes() { cout << "Yes"; }
inline void PrintNo() { cout << "No"; }

const double eps = 1e-7;
const int Nmax = 5e5 + 10;

int N, M;
vector< int > G[Nmax];
bool viz[Nmax];
stack< int > solutie;

void DFS(int node)
{
    viz[node] = true;

    for(auto it: G[node])
        if(viz[it] == false)
           DFS(it);

    solutie.push(node + 1);
}

int main()
{
	debugMode();

    cin >> N >> M;

    for(int i = 1; i <= M; ++i) {
        int x, y;
        cin >> x >> y;
        G[x - 1].push_back(y - 1);
    }

    for(int i = 1; i <= N; ++i)
        if(viz[i - 1] == false)
            DFS(i - 1);

    while(!solutie.empty()) {
        cout << solutie.top() << " ";
        solutie.pop();
    }

	return 0;
}