Cod sursa(job #2685166)

Utilizator TheGodFather2131Alexandru Miclea TheGodFather2131 Data 16 decembrie 2020 10:25:00
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
//ALEXANDRU MICLEA

#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <cstring>
#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>
#include <assert.h>

using namespace std;
using ll = long long;

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

//VARIABLES

vector <vector <int>> gr(100005);
vector <int> ans;
int used[100005];

//FUNCTIONS

void dfs(int nod) {
    used[nod] = 1;

    for (auto& x : gr[nod]) {
        if (!used[x]) dfs(x);
    }

    ans.push_back(nod);
}

//MAIN

int main() {

    int n, m; cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int x, y; cin >> x >> y;

        gr[x].push_back(y);
    }

    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;
}