Cod sursa(job #2084266)

Utilizator Teodor.mTeodor Marchitan Teodor.m Data 8 decembrie 2017 21:13:23
Problema Sortare topologica Scor 0
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];
int color[Nmax];
queue< int > solutie;

void DFS(int node)
{
    color[node + 1] = 1;
    solutie.push(node  + 1);

    for(auto it: G[node])
        if(color[it + 1] == 0)
           DFS(it);
}

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(color[i] == 0)
            DFS(i - 1);

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

	return 0;
}