Problem p02799 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

struct edge {
  ll v, idx, w;
};

ll N, M;
vector<ll> D;
vector<edge> es[100010];
ll res[100010], resC[200010];
bool solved[100010];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> M;
  D.resize(N);
  rep(i, N) cin >> D[i];
  rep(i, M) {
    ll u, v;
    cin >> u >> v;
    u--;
    v--;
    if (D[u] < D[v] || (D[u] == D[v] && u < v))
      swap(u, v);
    es[u].push_back((edge){v, i, D[u]});
    res[i] = D[u];
  }
  priority_queue<P, vector<P>, greater<P>> que;
  rep(i, N) { que.push(P(D[i], i)); }
  while (que.size()) {
    P p = que.top();
    que.pop();
    ll v = p.se;
    ll d = p.fi;
    bool ok = true;
    for (edge e : es[v]) {
      if (!solved[e.v] && D[e.v] != d) {
        ok = false;
      }
    }
    if (!ok) {
      cout << -1 << endl;
      return 0;
    }
    if (es[v].size() == 0) {
      continue;
    }
    bool col = false;
    for (edge e : es[v]) {
      solved[e.v] = true;
      if (resC[e.v] == 0)
        col = true;
    }
    resC[v] = col;
    solved[v] = true;
  }
  rep(i, N) {
    if (!solved[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  rep(i, N) {
    if (resC[i] == 0)
      cout << "W";
    else
      cout << "B";
  }
  cout << "\n";
  rep(i, M) { cout << res[i] << "\n"; }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

struct edge {
  ll v, idx, w;
};

ll N, M;
vector<ll> D;
vector<edge> es[200010];
ll res[200010], resC[200010];
bool solved[200010];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> M;
  D.resize(N);
  rep(i, N) cin >> D[i];
  rep(i, M) {
    ll u, v;
    cin >> u >> v;
    u--;
    v--;
    if (D[u] < D[v] || (D[u] == D[v] && u < v))
      swap(u, v);
    es[u].push_back((edge){v, i, D[u]});
    res[i] = D[u];
  }
  priority_queue<P, vector<P>, greater<P>> que;
  rep(i, N) { que.push(P(D[i], i)); }
  while (que.size()) {
    P p = que.top();
    que.pop();
    ll v = p.se;
    ll d = p.fi;
    bool ok = true;
    for (edge e : es[v]) {
      if (!solved[e.v] && D[e.v] != d) {
        ok = false;
      }
    }
    if (!ok) {
      cout << -1 << endl;
      return 0;
    }
    if (es[v].size() == 0) {
      continue;
    }
    bool col = false;
    for (edge e : es[v]) {
      solved[e.v] = true;
      if (resC[e.v] == 0)
        col = true;
    }
    resC[v] = col;
    solved[v] = true;
  }
  rep(i, N) {
    if (!solved[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  rep(i, N) {
    if (resC[i] == 0)
      cout << "W";
    else
      cout << "B";
  }
  cout << "\n";
  rep(i, M) { cout << res[i] << "\n"; }

  return 0;
}