Advertisement
gertsog

Gggg

Nov 19th, 2019
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include <fstream>
  2. #include <algorithm>
  3. #include <vector>
  4.  
  5. typedef std::vector<int> iVec;
  6. typedef std::vector<iVec> matr;
  7.  
  8. static matr G;
  9.  
  10. iVec obtain_cycle()
  11. {
  12.   int i = 0;
  13.   static int c = 0;
  14.   const int n = G.size();
  15.   iVec que(n);
  16.  
  17.   std::generate(que.begin(), que.end(), [] { return c++; });
  18.  
  19.   for (int k = 0; k < n * (n - 1); k++)
  20.   {
  21.     auto
  22.       v0 = G[que[0]],
  23.       v1 = G[que[1]];
  24.  
  25.     if (!v0[que[1]])
  26.     {
  27.       for (i = 2; !v0[que[i]] || !v1[que[i + 1]]; i++);
  28.       std::reverse(que.begin() + 1, que.begin() + i);
  29.     }
  30.  
  31.     que.push_back(std::forward<int>(que[0]));
  32.     que.erase(que.begin());
  33.   }
  34.  
  35.   return que;
  36. }
  37.  
  38. template <class type>
  39. std::ostream& operator<<( std::ostream & Out, const std::vector<type> & Vec )
  40. {
  41.   for (auto & a : Vec)
  42.     Out << a + 1 << " ";
  43.   return Out;
  44. }
  45.  
  46. int main()
  47. {
  48.   int n, is_e;
  49.   std::ifstream infile("fullham.in");
  50.  
  51.   infile >> n;
  52.  
  53.   G = std::forward<matr>(matr(n, std::forward<iVec>(iVec(n , 0))));
  54.   for (int u = 1; u < n; u++)
  55.     for (int v = 0; v < u; v++)
  56.       infile >> is_e, G[u][v] = G[v][u] = is_e;
  57.  
  58.   std::move(std::ofstream("fullham.out")) << obtain_cycle();
  59.  
  60.   return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement