Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef pair< int , int > PII;
- int n, a[100100], x, b[100100], pas[100100], mx;
- string s;
- multiset < pair < int , int > > M, W;
- vector < int > V[100100];
- bitset < 100100 > B;
- void print (int x) {
- B[x] = 1;
- for (auto it : V[x]) {
- if (B[it]) continue;
- cout << x << " " << it << "\n";
- print (it);
- }
- }
- bitset < 100100 > viz;
- void dfs(int x)
- {
- viz[x] = 1;
- for (auto it : V[x])
- if (!viz[it]) pas[it] = pas[x] + 1, mx = max(pas[it], mx), dfs(it);
- }
- int main(){
- ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
- cin >> n >> x;
- for (int i = 1; i <= n; i++) {
- cin >> a[i];
- M.insert({a[i], i});
- }
- int st = x / 2 + 1;
- int dr = st;
- b[1] = M.begin()->second;
- M.erase(M.begin());
- b[x + 1] = M.begin()->second;
- M.erase(M.begin());
- if (st == 1) {
- V[b[1]].push_back(b[2]);
- for (int i = 2; i <= x; i++) {
- V[b[i]].push_back(b[i - 1]);
- V[b[i]].push_back(b[i + 1]);
- }
- V[b[x + 1]].push_back(b[x]);
- print(1);
- return 0;
- }
- b[st] = M.rbegin()->second;
- int cp = M.rbegin()->first - 2;
- if (cp > 0) {
- W.insert({cp, M.rbegin()->second});
- }
- M.erase(*M.rbegin());
- if (x % 2 == 1) {
- ++dr;
- b[dr] = M.rbegin()->second;
- cp = M.rbegin()->first - 2;
- if (cp > 0) {
- W.insert({cp, M.rbegin()->second});
- }
- M.erase(*M.rbegin());
- }
- while (st > 2) {
- st--; dr++;
- b[st] = M.rbegin()->second;
- cp = M.rbegin()->first - 2;
- if (cp > 0) {
- W.insert({cp, M.rbegin()->second});
- }
- M.erase(*M.rbegin());
- b[dr] = M.rbegin()->second;
- cp = M.rbegin()->first - 2;
- if (cp > 0) {
- W.insert({cp, M.rbegin()->second});
- }
- M.erase(*M.rbegin());
- }
- V[b[1]].push_back(b[2]);
- for (int i = 2; i <= x; i++) {
- V[b[i]].push_back(b[i - 1]);
- V[b[i]].push_back(b[i + 1]);
- }
- V[b[x + 1]].push_back(b[x]);
- // for (auto it : W) {
- // cout << it.first << " " << it.second << "\n";
- // }
- while (M.size()) {
- pair < int , int > node = *M.rbegin();
- pair < int , int > dad = *W.rbegin();
- M.erase(*M.rbegin());
- W.erase(*W.rbegin());
- V[dad.second].push_back(node.second);
- V[node.second].push_back(dad.second);
- if (node.first > 1) {
- W.insert({node.first - 1, node.second});
- }
- if (dad.first > 1) {
- W.insert({dad.first - 1, dad.second});
- }
- }
- // testing
- pas[1] = 0;
- int node;
- dfs(1);
- for (int i = 1; i <= n; i++)
- if (pas[i] == mx){node = i; break;}
- mx = 1;
- memset(pas, 0, sizeof(pas));
- viz.reset();
- pas[node] = 1;
- dfs(node);
- // cout << mx - 1 << "\n";
- // assert(W.size() == 0);
- // --------------------------------------
- print(1);
- // cout << W.size() << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement