Problem p03249 (C++)

Buggy submission (status: Runtime Error):
// LightOj Pass : qavajxyq

#include <bits/stdc++.h>
#define deb(x) cerr << (#x) << " : " << x << "\n"
#define ll long long int
#define ld long double
using namespace std;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;

ll powmod(ll a, ll b, ll mod = MOD) {
  ll res = 1ll;
  b %= (mod - 1ll), a %= mod;
  while (b) {
    if (b % 2 == 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b /= 2;
  }
  return res;
}

ll d[300000];
ll n;

bool comp(int a, int b) { return d[a] < d[b]; }

vector<vector<int>> edges;
int cnt = 0;
ll res = 0;
ll tree[400000];
bool visited[4000000];
bool state = true;

void dfs(int u, int par = -1, ll dpth = 0) {
  cnt++;
  res += dpth;
  tree[u] = 1;
  visited[u] = true;
  int tmp = 0;
  for (auto v : edges[u]) {
    if (!visited[v]) {
      dfs(v, u, dpth + 1ll);
      tree[u] += tree[v];
    } else {
      tmp++;
    }
  }

  if (tmp > 1)
    state = false;
}

void dfs2(int u, int par = -1, ll ans = res) {
  if (ans != d[u])
    state = false;
  for (auto v : edges[u]) {
    if (v != par)
      dfs2(v, u, ans - tree[v] * 2 + n);
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("D:\\V S Code\\cpp\\competitiveProgramming\\Input.txt", "r", stdin);
  freopen("D:\\V S Code\\cpp\\competitiveProgramming\\OPT.txt", "w", stdout);
#endif

  ios::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  edges.resize(n + 1);
  memset(visited, false, sizeof(visited));
  int arr[n];
  map<ll, int> mp;
  ll sub[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> d[i], arr[i - 1] = i, mp[d[i]] = i, sub[i] = 1;

  sort(arr, arr + n, comp);
  bool vis[n + 1];
  memset(vis, false, sizeof(vis));

  int cur, par;
  int parent[n];

  vector<pair<int, int>> edge;
  for (int i = n - 1; i >= 0; i--) {

    if (vis[arr[i]])
      continue;
    cur = arr[i];
    deb(cur);
    vis[cur] = true;
    while (cur != 0) {
      par = 0;
      if (mp.count(d[cur] + (2 * sub[cur]) - n) and
          !vis[mp[d[cur] + (2 * sub[cur]) - n]])
        par = mp[d[cur] + (2 * sub[cur]) - n];
      if (par)
        sub[par] += sub[cur], edges[cur].push_back(par),
            edges[par].push_back(cur), vis[cur] = true,
                                       edge.push_back({cur, par});
      cur = par;
      deb(sub[par]);
      deb(par);
    }
  }

  dfs(arr[0]);
  if (cnt != n or res != d[arr[0]] or !state) {
    cout << -1;
    return 0;
  }

  dfs2(arr[0]);
  if (!state) {
    cout << -1;
    return 0;
  }

  for (auto x : edge)
    cout << x.first << " " << x.second << "\n";
}

Fixed submission:
// LightOj Pass : qavajxyq

#include <bits/stdc++.h>
#define deb(x) cerr << (#x) << " : " << x << "\n"
#define ll long long int
#define ld long double
using namespace std;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;

ll powmod(ll a, ll b, ll mod = MOD) {
  ll res = 1ll;
  b %= (mod - 1ll), a %= mod;
  while (b) {
    if (b % 2 == 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b /= 2;
  }
  return res;
}

ll d[300000];
ll n;

bool comp(int a, int b) { return d[a] < d[b]; }

vector<vector<int>> edges;
int cnt = 0;
ll res = 0;
ll tree[400000];
bool visited[4000000];
bool state = true;

void dfs(int u, int par = -1, ll dpth = 0) {
  cnt++;
  res += dpth;
  tree[u] = 1;
  visited[u] = true;
  int tmp = 0;
  for (auto v : edges[u]) {
    if (!visited[v]) {
      dfs(v, u, dpth + 1ll);
      tree[u] += tree[v];
    } else {
      tmp++;
    }
  }

  if (tmp > 1)
    state = false;
}

void dfs2(int u, int par = -1, ll ans = res) {
  if (ans != d[u])
    state = false;
  for (auto v : edges[u]) {
    if (v != par)
      dfs2(v, u, ans - tree[v] * 2 + n);
  }
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n;
  edges.resize(n + 1);
  memset(visited, false, sizeof(visited));
  int arr[n];
  map<ll, int> mp;
  ll sub[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> d[i], arr[i - 1] = i, mp[d[i]] = i, sub[i] = 1;

  sort(arr, arr + n, comp);
  bool vis[n + 1];
  memset(vis, false, sizeof(vis));

  int cur, par;
  int parent[n];

  vector<pair<int, int>> edge;
  for (int i = n - 1; i >= 0; i--) {

    if (vis[arr[i]])
      continue;
    cur = arr[i];
    deb(cur);
    vis[cur] = true;
    while (cur != 0) {
      par = 0;
      if (mp.count(d[cur] + (2 * sub[cur]) - n) and
          !vis[mp[d[cur] + (2 * sub[cur]) - n]])
        par = mp[d[cur] + (2 * sub[cur]) - n];
      if (par)
        sub[par] += sub[cur], edges[cur].push_back(par),
            edges[par].push_back(cur), vis[cur] = true,
                                       edge.push_back({cur, par});
      cur = par;
      deb(sub[par]);
      deb(par);
    }
  }

  dfs(arr[0]);
  if (cnt != n or res != d[arr[0]] or !state) {
    cout << -1;
    return 0;
  }

  dfs2(arr[0]);
  if (!state) {
    cout << -1;
    return 0;
  }

  for (auto x : edge)
    cout << x.first << " " << x.second << "\n";
}