Cod sursa(job #2646748)

Utilizator MatteoalexandruMatteo Verzotti Matteoalexandru Data 1 septembrie 2020 21:30:36
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.69 kb
// intai se invata fluxul si apoi sortarea topologica
/*
                `-/oo+/-   ``
              .oyhhhhhhyo.`od
             +hhhhyyoooos. h/
            +hhyso++oosy- /s
           .yoooossyyo:``-y`
            ..----.` ``.-/+:.`
                   `````..-::/.
                  `..```.-::///`
                 `-.....--::::/:
                `.......--::////:
               `...`....---:::://:
             `......``..--:::::///:`
            `---.......--:::::////+/`
            ----------::::::/::///++:
            ----:---:::::///////////:`
            .----::::::////////////:-`
            `----::::::::::/::::::::-
             `.-----:::::::::::::::-
               ...----:::::::::/:-`
                 `.---::/+osss+:`
                   ``.:://///-.
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cmath>

using namespace std;

const int INF = 2e9;
const int N = 5e4;

int n, m;

vector <int> G[5 + N];
vector <int> sol;
bool viz[5 + N];

void DFS(int node) {
    viz[node] = true;
    for(auto it : G[node]) {
        if(!viz[it])
            DFS(it);
    }
    sol.push_back(node);
}

int main() {
    freopen("sortaret.in", "r", stdin);
    freopen("sortaret.out", "w", stdout);

    scanf("%d%d", &n, &m);

    for(int i = 1; i <= m; i++) {
        int x, y;
        scanf("%d%d", &x, &y);
        G[x].push_back(y);
    }

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

    reverse(sol.begin(), sol.end());
    for(auto it : sol) printf("%d ", it);
    return 0;
}