Cod sursa(job #2810389)

Utilizator MR0L3eXMaracine Constantin Razvan MR0L3eX Data 29 noiembrie 2021 13:07:18
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.04 kb
#include "bits/stdc++.h"

using namespace std;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define int long long

using ll = long long;
using ull = unsigned long long;

const char nl = '\n';
const int mxN = 5e4;
const int INF = 1e9;

vector<bool> been(mxN);
vector<int> ans;
vector<int> adj[mxN];

void dfs(int nod) {
    been[nod] = true;
    for (int u : adj[nod]) {
        if (been[u]) continue;
        dfs(u);
    }
    ans.push_back(nod);
}

void topo_sort(int n) {
    for (int i = 0; i < n; ++i) {
        if (been[i]) continue;
        dfs(i);
    }
    reverse(ans.begin(), ans.end());
}

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

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

    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int x, y;
        cin >> x >> y;
        --x, --y;
        adj[x].push_back(y);
    }

    topo_sort(n);

    for (int x : ans) {
        cout << x + 1 << ' ';
    }
    return 0;

}