Advertisement
Guest User

B

a guest
Sep 27th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4. #include <memory.h>
  5.  
  6. using namespace std;
  7.  
  8. bool uses[100000];
  9. vector<int> g[100000];
  10. vector<int> res;
  11.  
  12. bool dfs(int a)
  13. {
  14.     if(uses[a])
  15.     {
  16.         return true;
  17.     }
  18.     uses[a] = true;
  19.     for(auto x:g[a])
  20.     {
  21.         cout << x << "____\n";
  22.         if(dfs(x))
  23.             res.push_back(x);
  24.     }
  25. }
  26.  
  27. int main()
  28. {
  29.     int n, m, a, b;
  30.     cin >> n >> m;
  31.     for(int i = 0; i < m; i++)
  32.     {
  33.         cin >> a >> b;
  34.         g[a].push_back(b);
  35.     }
  36.     /*for(int i = 1; i <= n; i++)
  37.     {
  38.         cout <<endl<< i<<": ";
  39.         for(auto x: g[i])
  40.         {
  41.             cout << x << ' ';
  42.         }
  43.        
  44.     }*/
  45.     for(int i = 1; i <= n; i++)
  46.     {
  47.         if(dfs(i+1))
  48.         {
  49.             res.push_back(i+1);
  50.             cout << "YES\n";
  51.             for(auto x:res)
  52.             {
  53.                 cout << x <<' ';
  54.             }
  55.             return 0;
  56.         }
  57.         memset(uses, 0, sizeof(uses));
  58.     }
  59.     cout << "NO";
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement