fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #ifdef LOCAL
  5. #define DEBUG(...) debug(#__VA_ARGS__, __VA_ARGS__)
  6. #else
  7. #define DEBUG(...) 6
  8. #endif
  9.  
  10. template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) {return os << "(" << p.first << ", " << p.second << ")";}
  11. template<typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type* = nullptr>
  12. ostream& operator << (ostream &os, const C &c) {bool f = true; os << "["; for (const auto &x : c) {if (!f) os << ", "; f = false; os << x;} return os << "]";}
  13. template<typename T> void debug(string s, T x) {cerr << s << " = " << x << "\n";}
  14. template <typename T, typename... Args> void debug(string s, T x, Args... args) {for (int i=0, b=0; i<(int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++; else
  15. if (s[i] == ')' || s[i] == '}') b--; else if (s[i] == ',' && b == 0) {cerr << s.substr(0, i) << " = " << x << " | "; debug(s.substr(s.find_first_not_of(' ', i + 1)), args...); break;}}
  16.  
  17. int main() {
  18. ios_base::sync_with_stdio(false);
  19. cin.tie(NULL);
  20.  
  21. mt19937 rng((uint32_t) chrono::steady_clock::now().time_since_epoch().count());
  22. // mt19937 rng(6);
  23.  
  24. int n, m;
  25. cin >> n >> m;
  26. vector<vector<int>> adj(n);
  27. vector<vector<bool>> mat(n, vector<bool>(n));
  28. for (int i=0; i<m; i++) {
  29. int u, v;
  30. cin >> u >> v;
  31. u--, v--;
  32. adj[u].push_back(v);
  33. adj[v].push_back(u);
  34. mat[u][v] = mat[v][u] = true;
  35. }
  36.  
  37. if (n <= 20) {
  38. auto solve = [&] (auto &self, int u, int mask) -> bool {
  39. if (mask == (1 << n) - 1) {
  40. if (u == 0) {
  41. cout << u + 1 << " ";
  42. return true;
  43. }
  44. return false;
  45. }
  46. for (int v : adj[u])
  47. if (~mask & (1 << v) && self(self, v, mask | (1 << v))) {
  48. cout << u + 1 << " ";
  49. return true;
  50. }
  51. return false;
  52. };
  53. solve(solve, 0, 0);
  54. cout << "\n";
  55. return 0;
  56. }
  57.  
  58. for (int rep=0; rep<100; rep++) {
  59. vector<int> ret(n);
  60. iota(ret.begin() + 1, ret.end(), 1);
  61. shuffle(ret.begin() + 1, ret.end(), rng);
  62. ret.push_back(0);
  63. bool done = false;
  64. if (rep == 99)
  65. done = true;
  66. while (!done) {
  67. done = true;
  68. for (int i=0; i<n; i++)
  69. if (!mat[ret[i]][ret[i+1]]) {
  70. bool ok = false;
  71. for (int j=i+1; j<n; j++)
  72. if (mat[ret[j]][ret[j+1]] && mat[ret[i]][ret[j]] && mat[ret[i+1]][ret[j+1]]) {
  73. reverse(ret.begin() + i + 1, ret.begin() + j + 1);
  74. ok = true;
  75. break;
  76. }
  77. if (!ok) {
  78. for (int j=0; j+1<i; j++)
  79. if (mat[ret[j]][ret[j+1]] && mat[ret[i]][ret[j]] && mat[ret[i+1]][ret[j+1]]) {
  80. int l = i + 1, r = j;
  81. vector<bool> vis(n);
  82. while (!vis[l] && !vis[r]) {
  83. swap(ret[l], ret[r]);
  84. vis[l] = vis[r] = true;
  85. if (++l == n)
  86. l = 0;
  87. if (--r == -1)
  88. r = n - 1;
  89. }
  90. ret[n] = ret[0];
  91. ok = true;
  92. break;
  93. }
  94. if (!ok)
  95. goto fail;
  96. }
  97. done = false;
  98. }
  99. }
  100. for (int i=0; i<=n; i++)
  101. cout << ret[i] + 1 << " ";
  102. cout << "\n";
  103. return 0;
  104. fail:;
  105. }
  106.  
  107. return 0;
  108. }
  109.  
Success #stdin #stdout 0s 5064KB
stdin
Standard input is empty
stdout
1