Cod sursa(job #2419830)

Utilizator Alex18maiAlex Enache Alex18mai Data 9 mai 2019 16:17:11
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.25 kb
//ALEX ENACHE

#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <time.h>
#include <iomanip>
#include <deque>
#include <math.h>
#include <cmath>
#include <assert.h>
#include <stack>
#include <bitset>
#include <random>
#include <chrono>

using namespace std;

//#include <iostream>
#include <fstream>
ifstream cin ("sortaret.in");ofstream cout ("sortaret.out");

vector < int  > gr[50100];
vector < int > ans;
int used[50100];

void dfs (int nod){
    used[nod] = 1;
    for (auto &x : gr[nod]){
        if (!used[x]){
            dfs(x);
        }
    }
    ans.push_back(nod);
}

int main() {

    //freopen("input", "r", stdin);freopen("output", "w", stdout);

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cout << setprecision(10) << fixed;
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    srand(time(nullptr));

    int n , m;
    cin>>n>>m;

    for (int i=1; i<=n; i++){
        int a , b;
        cin>>a>>b;
        gr[a].push_back(b);
    }

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

    reverse (ans.begin() , ans.end());

    for (auto &x : ans){
        cout<<x<<" ";
    }

    return 0;
}