Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. /**
  2. * code generated by JHelper
  3. * More info: https://github.com/AlexeyDmitriev/JHelper
  4. * @author gainullin.ildar
  5. */
  6.  
  7. #include <bits/stdc++.h>
  8.  
  9. #include <cmath>
  10. #include <iostream>
  11. #include <vector>
  12. #include <algorithm>
  13. #include <string>
  14. #include <set>
  15. #include <map>
  16. #include <list>
  17. #include <time.h>
  18. #include <math.h>
  19. #include <random>
  20. #include <deque>
  21. #include <queue>
  22. #include <cassert>
  23. #include <unordered_map>
  24. #include <iomanip>
  25.  
  26. using namespace std;
  27.  
  28.  
  29. typedef long long ll;
  30. typedef unsigned long long ull;
  31.  
  32. const int N = 4000 + 7;
  33.  
  34. int g[N][N];
  35. int deg[N];
  36. int n;
  37.  
  38. vector<int> rec(int ed)
  39. {
  40. if (ed == n * (n - 1) / 2)
  41. {
  42. vector<int> a(n);
  43. for (int i = 0; i < n; i++)
  44. {
  45. a[i] = i;
  46. }
  47. return a;
  48. }
  49. else
  50. {
  51. int x = -1, y = -1;
  52. int res = 0;
  53. for (int i = 0; i < n; i++)
  54. {
  55. for (int j = i + 1; j < n; j++)
  56. {
  57. if (!g[i][j] && deg[i] + deg[j] > res)
  58. {
  59. x = i, y = j;
  60. res = deg[i] + deg[j];
  61. }
  62. }
  63. }
  64. g[x][y] = g[y][x] = 1;
  65. auto a = rec(ed + 1);
  66. int pos = -1;
  67. for (int i = 0; i < n; i++)
  68. {
  69. if ((a[i] == x && a[(i + 1) % n] == y) || (a[i] == y && a[(i + 1) % n] == x))
  70. {
  71. pos = i;
  72. break;
  73. }
  74. }
  75. g[x][y] = g[y][x] = 0;
  76. if (pos == -1)
  77. {
  78. return a;
  79. }
  80. else
  81. {
  82. rotate(a.begin(), a.begin() + (pos + 1) % n, a.end());
  83. for (int i = 1; i < n - 2; i++)
  84. {
  85. if (g[a[0]][a[i + 1]] && g[a[i]][a[n - 1]])
  86. {
  87. vector<int> ans;
  88. ans.push_back(a[0]);
  89. ans.push_back(a[i + 1]);
  90. for (int j = i + 2; j < n; j++)
  91. {
  92. ans.push_back(a[j]);
  93. }
  94. ans.push_back(a[i]);
  95. for (int x = i - 1; x > 0; x--)
  96. {
  97. ans.push_back(a[x]);
  98. }
  99. return ans;
  100. }
  101. }
  102. assert(0);
  103. }
  104. }
  105. }
  106.  
  107. class Main
  108. {
  109. public:
  110. void solve(std::istream &in, std::ostream &out)
  111. {
  112. srand(time(0));
  113. in >> n;
  114. for (int i = 0; i < n; i++)
  115. {
  116. for (int j = 0; j < n; j++)
  117. {
  118. g[i][j] = 0;
  119. }
  120. }
  121. int ans = 0;
  122. for (int i = 0; i < n; i++)
  123. {
  124. for (int j = 0; j < i; j++)
  125. {
  126. char c;
  127. in >> c;
  128. g[i][j] = g[j][i] = (c == '1');
  129. ans += g[i][j];
  130. deg[i] += g[i][j];
  131. deg[j] += g[j][i];
  132. }
  133. }
  134. vector<int> res = rec(ans);
  135. for (int i = 0; i < n; i++)
  136. {
  137. out << res[i] + 1 << ' ';
  138. }
  139. out << '\n';
  140. }
  141. };
  142.  
  143. int main()
  144. {
  145. ios::sync_with_stdio(0);
  146. Main solver;
  147. std::istream &in(std::cin);
  148. std::ostream &out(std::cout);
  149. solver.solve(in, out);
  150. return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement