Cod sursa(job #2330823)

Utilizator StefanZamfirStefan Zamfir StefanZamfir Data 28 ianuarie 2019 20:54:53
Problema Sortare topologica Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.23 kb

/*


      _             _    ___     ___    _  __       __
   __| |   __ _    / |  / _ \   / _ \  (_)/ /    _  \ \
  / _` |  / _` |   | | | | | | | | | |   / /    (_)  | |
 | (_| | | (_| |   | | | |_| | | |_| |  / /_     _   | |
  \__,_|  \__,_|   |_|  \___/   \___/  /_/(_)   (_)  | |
                                                    /_/


 */

//#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <set>
#include <algorithm>
#include <bitset>
#include <time.h>
#include <tuple>
#include <fstream>
#include <iomanip>
#include <utility>
#include <list>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/trie_policy.hpp>
#include <unordered_map>

#pragma warning "da 100% din tine. :)"
#define nl '\n'
#define cnl cout << '\n';
#define pb(x) push_back(x)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define ll long long
#define ull unsigned ll

#define a first
#define b second
#ifdef INFOARENA
#define ProblemName "sortaret"
#endif
#define MCONCAT(A, B) A B
#ifdef ProblemName
#define InFile MCONCAT(ProblemName, ".in")
#define OuFile MCONCAT(ProblemName, ".out")
#else
#define InFile "a.in"
#define OuFile "a.out"
#endif


//using namespace __gnu_pbds;
using namespace std;
ifstream cin(InFile);
ofstream cout(OuFile);


//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
//typedef trie<string, null_type, trie_string_access_traits<>, pat_trie_tag, trie_prefix_search_node_update> pref_trie;

template<class v, class type>
void print(v Vector, type nr) {
    for_each(all(Vector), [](type x) {
        cout << x << ' ';
    });
}


struct vertex {
    bool vis = 0;
    list<int> adjlist;
};

stack<int> ans;

vertex v[100001];

void add(vertex &target, int to_add_index) {
    target.adjlist.push_back(to_add_index);
}

void dfs(int start_index) {
    v[start_index].vis = true;
    for (auto it = v[start_index].adjlist.begin(); it != v[start_index].adjlist.end(); ++it) {
        //parcurgem dfs, surpriza
        //*it = indexul urmatorului nod din lista
        if (!v[*it].vis) {
            dfs(*it);
        }
    }
    ans.push(start_index);
}
//mare grija aici, daca pui si int vis in struct iese din timp!
//void bfs(int start_index) {
//    queue<int> q;
//
//    q.push(start_index);
//    dist[start_index] = 0;
//    int cindex;
//
//    while (!q.empty()) {
//        cindex = q.front();
//
//        for (auto it = v[cindex].adjlist.begin(); it != v[cindex].adjlist.end(); ++it) {
//            if (dist[*it] == 0 && *it != start_index) {
//                dist[*it] = dist[cindex] + 1;
//                q.push(*it);
//            }
//        }
//        q.pop();
//    }
//
//}


int main() {
    ios_base::sync_with_stdio(false);
//    clock_t tStart = clock();
    cin.tie(0);
    int x, y;
    int n, m, s;
    int cc = 0;
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        cin >> x >> y;
        add(v[x], y);
    }
    for (int i = 1; i <= n; ++i) {
        if (v[i].vis == 0){
            dfs(i);
        }
    }
    while(!ans.empty()){
        cout << ans.top() << ' ';
        ans.pop();
    }
    cout << nl;
//    printf("\nTime taken: %.2fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
}