Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest May 16th, 2018 101 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <stack>
  4. #include <vector>
  5. #include <cstring>
  6. #include <algorithm>
  7. using namespace std;
  8.  
  9. int n, m;
  10. vector <int> v[32002];
  11. stack <int> st;
  12. bool visited[32002];
  13.  
  14. bool flag;
  15. void dfs(int here) {
  16.  
  17.     visited[here] = true;
  18.     for (int i = 0; i < v[here].size(); i++) {
  19.         if (!visited[v[here][i]])dfs(v[here][i]);  
  20.     }
  21.     st.push(here);
  22. }
  23. int main() {
  24.    
  25.     memset(visited, 0, sizeof(visited));
  26.     scanf("%d %d\n", &n, &m);
  27.     for (int i = 0; i < m; i++) {
  28.         int a, b; scanf("%d %d", &a, &b);
  29.         v[a].push_back(b); // a -> b
  30.     }
  31.     for (int i = 1; i <= n; i++) {
  32.         if (!visited[i])dfs(i);
  33.     }
  34.     while (!st.empty()) {
  35.         printf("%d ", st.top());
  36.         st.pop();
  37.     }
  38.     printf("\n");
  39.  
  40. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top