Problem p02799 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)

template <class T> using minheap = priority_queue<T, vector<T>, greater<T>>;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> D(N);
  rep(i, N) cin >> D[i];
  vector<int> U(M), V(M);
  vector<vector<int>> G(N);
  rep(i, M) {
    cin >> U[i] >> V[i];
    U[i]--;
    V[i]--;
    G[U[i]].push_back(i);
    G[V[i]].push_back(i);
  }
  vector<set<int>> T(N);
  vector<bool> vis(N);
  vector<int> color(N, -1);
  constexpr int INF = 1e9;
  vector<int> ans(N, INF + 1);
  rep(i, N) {
    pair<int, int> mn(INF, INF);
    for (int k : G[i]) {
      int j = U[k] ^ V[k] ^ i;
      mn = min(mn, make_pair(D[j], k));
    }
    ans[mn.second] = D[i];
  }
  rep(i, N) if (color[i] == -1) {
    color[i] = 0;
    queue<int> q;
    q.push(i);
    while (not q.empty()) {
      int v = q.front();
      q.pop();
      for (int j : G[v])
        if (ans[j] <= INF) {
          int u = U[j] ^ V[j] ^ v;
          if (color[u] == -1) {
            color[u] = color[v] ^ 1;
            q.push(u);
          }
        }
    }
  }
  rep(i, M) ans[i] = min(ans[i], INF);
  for (int ii = 0; ii < 2; ii++) {
    constexpr ll INFL = 1e18;
    vector<ll> dist(N, INFL);
    minheap<pair<ll, int>> heap;
    rep(i, N) if (color[i] == ii) {
      dist[i] = 0;
      heap.emplace(0, i);
    }
    while (not heap.empty()) {
      ll d;
      int u;
      tie(d, u) = heap.top();
      heap.pop();
      if (d > dist[u])
        continue;
      for (int i : G[u]) {
        int v = U[i] ^ V[i] ^ u;
        if (dist[v] > dist[u] + ans[i]) {
          dist[v] = dist[u] + ans[i];
          heap.emplace(dist[v], v);
        }
      }
    }
    rep(i, N) if (color[i] == 1 - ii and dist[i] != D[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  rep(i, N) cout << (color[i] ? 'B' : 'W');
  cout << endl;
  rep(i, M) { cout << ans[i] << '\n'; }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)

template <class T> using minheap = priority_queue<T, vector<T>, greater<T>>;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> D(N);
  rep(i, N) cin >> D[i];
  vector<int> U(M), V(M);
  vector<vector<int>> G(N);
  rep(i, M) {
    cin >> U[i] >> V[i];
    U[i]--;
    V[i]--;
    G[U[i]].push_back(i);
    G[V[i]].push_back(i);
  }
  vector<set<int>> T(N);
  vector<bool> vis(N);
  vector<int> color(N, -1);
  constexpr int INF = 1e9;
  vector<int> ans(M, INF + 1);
  rep(i, N) {
    pair<int, int> mn(INF, INF);
    for (int k : G[i]) {
      int j = U[k] ^ V[k] ^ i;
      mn = min(mn, make_pair(D[j], k));
    }
    ans[mn.second] = D[i];
  }
  rep(i, N) if (color[i] == -1) {
    color[i] = 0;
    queue<int> q;
    q.push(i);
    while (not q.empty()) {
      int v = q.front();
      q.pop();
      for (int j : G[v])
        if (ans[j] <= INF) {
          int u = U[j] ^ V[j] ^ v;
          if (color[u] == -1) {
            color[u] = color[v] ^ 1;
            q.push(u);
          }
        }
    }
  }
  rep(i, M) ans[i] = min(ans[i], INF);
  for (int ii = 0; ii < 2; ii++) {
    constexpr ll INFL = 1e18;
    vector<ll> dist(N, INFL);
    minheap<pair<ll, int>> heap;
    rep(i, N) if (color[i] == ii) {
      dist[i] = 0;
      heap.emplace(0, i);
    }
    while (not heap.empty()) {
      ll d;
      int u;
      tie(d, u) = heap.top();
      heap.pop();
      if (d > dist[u])
        continue;
      for (int i : G[u]) {
        int v = U[i] ^ V[i] ^ u;
        if (dist[v] > dist[u] + ans[i]) {
          dist[v] = dist[u] + ans[i];
          heap.emplace(dist[v], v);
        }
      }
    }
    rep(i, N) if (color[i] == 1 - ii and dist[i] != D[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  rep(i, N) cout << (color[i] ? 'B' : 'W');
  cout << endl;
  rep(i, M) { cout << ans[i] << '\n'; }
}