Problem p02799 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fo(i, a, b) for ((i) = (a); i <= (b); i++)
#define rfo(i, a, b) for ((i) = (a); i >= (b); i--)
#define inrange(x, y, z) (((x) >= (y)) && ((x) <= (z)))
#define ALL(vec) ((vec).begin(), (vec).end())
#define SOR(vec) sort(ALL(vec))
#define UNI(vec) (vec).erase(unique(ALL(vec)), (vec).end())
using namespace std;
int n, m, uu, vv, d[100100], c[200100], co[100100];
vector<int> ga[100100];
vector<pair<int, int>> cand[100100];
pair<int, int> nxt[100100];
void search(int u) {
  int v = nxt[u].first;
  if (!co[v])
    search(v);
  co[u] = 3 - co[v];
  c[nxt[u].second] = d[u];
}
int main() {
#ifdef FILIN
#ifndef DavidDesktop
  freopen(FILIN, "r", stdin);
  freopen(FILOUT, "w", stdout);
#endif
#endif
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> d[i];
  for (int i = 1; i <= m; i++) {
    cin >> uu >> vv;
    ga[uu].push_back(vv);
    ga[vv].push_back(uu);
    if (d[uu] >= d[vv])
      cand[uu].push_back(make_pair(vv, i));
    if (d[vv] >= d[uu])
      cand[vv].push_back(make_pair(uu, i));
  }
  for (int i = 1; i <= n; i++) {
    if (cand[i].size() == 0) {
      cout << -1 << endl;
      return 0;
    }
    nxt[i] = cand[i][0];
  }
  for (int i = 1; i <= n; i++) {
    int j = nxt[i].first;
    if (nxt[j].first == i) {
      c[nxt[i].second] = d[i];
      co[i] = 1;
      co[j] = 2;
    }
    search(i);
  }
  for (int i = 1; i <= n; i++) {
    if (co[i] == 1)
      cout << "W";
    else
      cout << "B";
  }
  cout << endl;
  for (int i = 1; i <= m; i++) {
    if (!c[i])
      c[i] = 1e9;
    cout << c[i] << endl;
  }
  cout << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define fo(i, a, b) for ((i) = (a); i <= (b); i++)
#define rfo(i, a, b) for ((i) = (a); i >= (b); i--)
#define inrange(x, y, z) (((x) >= (y)) && ((x) <= (z)))
#define ALL(vec) ((vec).begin(), (vec).end())
#define SOR(vec) sort(ALL(vec))
#define UNI(vec) (vec).erase(unique(ALL(vec)), (vec).end())
using namespace std;
int n, m, uu, vv, d[100100], c[200100], co[100100];
vector<int> ga[100100];
vector<pair<int, int>> cand[100100];
pair<int, int> nxt[100100];
void search(int u) {
  int v = nxt[u].first;
  if (!co[v])
    search(v);
  co[u] = 3 - co[v];
  c[nxt[u].second] = d[u];
}
int main() {
#ifdef FILIN
#ifndef DavidDesktop
  freopen(FILIN, "r", stdin);
  freopen(FILOUT, "w", stdout);
#endif
#endif
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> d[i];
  for (int i = 1; i <= m; i++) {
    cin >> uu >> vv;
    ga[uu].push_back(vv);
    ga[vv].push_back(uu);
    if (d[uu] >= d[vv])
      cand[uu].push_back(make_pair(vv, i));
    if (d[vv] >= d[uu])
      cand[vv].push_back(make_pair(uu, i));
  }
  for (int i = 1; i <= n; i++) {
    if (cand[i].size() == 0) {
      cout << -1 << endl;
      return 0;
    }
    nxt[i] = cand[i][0];
  }
  for (int i = 1; i <= n; i++) {
    int j = nxt[i].first;
    if (nxt[j].first == i) {
      c[nxt[i].second] = d[i];
      co[i] = 1;
      co[j] = 2;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!co[i]) {
      search(i);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (co[i] == 1)
      cout << "W";
    else
      cout << "B";
  }
  cout << endl;
  for (int i = 1; i <= m; i++) {
    if (!c[i])
      c[i] = 1e9;
    cout << c[i] << endl;
  }
  cout << endl;
  return 0;
}