Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define PI 2*asin(1)
  3. #define FOR(i,n) for(long long i = 0; i < n;i++)
  4. using namespace std;
  5.  
  6. vector<vector<int>>a(100000);
  7. //vector<vector<int>>b;
  8. string toBinStr(long long a){
  9.     return bitset<32>(a).to_string();
  10. }
  11. vector<int>used;
  12. vector <int>res;
  13. int p = -1;
  14. bool f = false;
  15. bool f1 = false;
  16. void dfs(int v){
  17.     used[v] = 1;
  18.     for(int i : a[v])
  19.         if (!used[i])
  20.             dfs(i);
  21.     used[v] = 2;
  22.     res.push_back(v+1);
  23. }
  24. int main(){
  25.     ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  26.     long long n,m;
  27.     cin >> n >> m;
  28.     FOR(i,n) used.push_back(0);
  29.     FOR(i,m){
  30.         int x,y; cin >> x >> y;
  31.         x--;y--;
  32.         a[x].push_back(y);
  33.     }
  34.     FOR(i,n)
  35.         if(!used[i])
  36.             dfs(i);
  37.     reverse(res.begin(),res.end());
  38.     for(int i : res) cout << i << ' ' ;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement