Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair< int , int > PII;
  7.  
  8. int n, a[100100], x, b[100100], pas[100100], mx;
  9. string s;
  10.  
  11. multiset < pair < int , int > > M, W;
  12. vector < int > V[100100];
  13. bitset < 100100 > B;
  14.  
  15. void print (int x) {
  16. B[x] = 1;
  17.  
  18. for (auto it : V[x]) {
  19. if (B[it]) continue;
  20.  
  21. cout << x << " " << it << "\n";
  22. print (it);
  23. }
  24. }
  25.  
  26. bitset < 100100 > viz;
  27. void dfs(int x)
  28. {
  29. viz[x] = 1;
  30. for (auto it : V[x])
  31. if (!viz[it]) pas[it] = pas[x] + 1, mx = max(pas[it], mx), dfs(it);
  32. }
  33.  
  34. int main(){
  35. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  36. cin >> n >> x;
  37. for (int i = 1; i <= n; i++) {
  38. cin >> a[i];
  39. M.insert({a[i], i});
  40. }
  41.  
  42. int st = x / 2 + 1;
  43. int dr = st;
  44.  
  45. b[1] = M.begin()->second;
  46. M.erase(M.begin());
  47.  
  48. b[x + 1] = M.begin()->second;
  49. M.erase(M.begin());
  50.  
  51. if (st == 1) {
  52. V[b[1]].push_back(b[2]);
  53. for (int i = 2; i <= x; i++) {
  54. V[b[i]].push_back(b[i - 1]);
  55. V[b[i]].push_back(b[i + 1]);
  56. }
  57. V[b[x + 1]].push_back(b[x]);
  58.  
  59. print(1);
  60. return 0;
  61. }
  62.  
  63. b[st] = M.rbegin()->second;
  64.  
  65. int cp = M.rbegin()->first - 2;
  66. if (cp > 0) {
  67. W.insert({cp, M.rbegin()->second});
  68. }
  69.  
  70. M.erase(*M.rbegin());
  71.  
  72. if (x % 2 == 1) {
  73. ++dr;
  74. b[dr] = M.rbegin()->second;
  75.  
  76. cp = M.rbegin()->first - 2;
  77. if (cp > 0) {
  78. W.insert({cp, M.rbegin()->second});
  79. }
  80.  
  81. M.erase(*M.rbegin());
  82. }
  83.  
  84. while (st > 2) {
  85. st--; dr++;
  86. b[st] = M.rbegin()->second;
  87.  
  88. cp = M.rbegin()->first - 2;
  89. if (cp > 0) {
  90. W.insert({cp, M.rbegin()->second});
  91. }
  92.  
  93. M.erase(*M.rbegin());
  94.  
  95. b[dr] = M.rbegin()->second;
  96. cp = M.rbegin()->first - 2;
  97. if (cp > 0) {
  98. W.insert({cp, M.rbegin()->second});
  99. }
  100.  
  101. M.erase(*M.rbegin());
  102. }
  103.  
  104. V[b[1]].push_back(b[2]);
  105. for (int i = 2; i <= x; i++) {
  106. V[b[i]].push_back(b[i - 1]);
  107. V[b[i]].push_back(b[i + 1]);
  108. }
  109. V[b[x + 1]].push_back(b[x]);
  110.  
  111. // for (auto it : W) {
  112. // cout << it.first << " " << it.second << "\n";
  113. // }
  114.  
  115.  
  116. while (M.size()) {
  117. pair < int , int > node = *M.rbegin();
  118. pair < int , int > dad = *W.rbegin();
  119.  
  120. M.erase(*M.rbegin());
  121. W.erase(*W.rbegin());
  122.  
  123. V[dad.second].push_back(node.second);
  124. V[node.second].push_back(dad.second);
  125.  
  126. if (node.first > 1) {
  127. W.insert({node.first - 1, node.second});
  128. }
  129.  
  130. if (dad.first > 1) {
  131. W.insert({dad.first - 1, dad.second});
  132. }
  133. }
  134.  
  135. // testing
  136. pas[1] = 0;
  137. int node;
  138. dfs(1);
  139. for (int i = 1; i <= n; i++)
  140. if (pas[i] == mx){node = i; break;}
  141. mx = 1;
  142. memset(pas, 0, sizeof(pas));
  143. viz.reset();
  144. pas[node] = 1;
  145. dfs(node);
  146.  
  147. // cout << mx - 1 << "\n";
  148. // assert(W.size() == 0);
  149.  
  150. // --------------------------------------
  151.  
  152. print(1);
  153. // cout << W.size() << "\n";
  154. return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement