Cod sursa(job #2084257)

Utilizator Teodor.mTeodor Marchitan Teodor.m Data 8 decembrie 2017 20:49:28
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, k;
vector< int > G[Nmax];
bool viz[Nmax];
vector< int > solutie;

void DFS(int node)
{
    viz[node] = true;
    if(node == 0)
        solutie.push_back(node + 1);

    for(auto it: G[node]) {
        if(viz[it] == false) {
           solutie.push_back(it + 1);
           viz[it] = true;
           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);
    }

    DFS(0);

    for(auto it: solutie)
        cout << it << " ";

	return 0;
}