Problem p02799 (C++)

Buggy submission (status: Runtime Error):
// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
using int64 = long long int;

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

// Union-Find 木 (Verified: AtCoder Typical Contest 001 B)
struct UnionFind {
private:
  const int n;
  int size_;
  vector<int> uf;

public:
  // 初期化 UnionFind uni(n) のように宣言すれば良い
  UnionFind(int _n) : n(_n), size_(_n), uf(_n, -1) {}
  // find (木の根を求める)
  int find(int x) { return (uf[x] < 0) ? x : uf[x] = find(uf[x]); }
  // x と y が同じ集合に属するかどうか
  bool same(int x, int y) { return find(x) == find(y); }
  // x が属する集合の要素数
  int size(int x) { return -uf[find(x)]; }
  // 集合はいくつあるか
  int size() { return size_; }
  // x と y の属する集合を併合
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    size_--;
    if (-uf[x] < -uf[y])
      swap(x, y);
    uf[x] += uf[y];
    uf[y] = x;
    return true;
  }
};

int main() {
  int N, M;
  scanf("%d%d", &N, &M);
  vector<ll> D(N);
  for (int i = 0; i < N; i++)
    scanf("%lld", &D[i]);

  vector<vector<int>> G(N);
  map<pair<int, int>, ll> edge_map;
  for (int i = 0; i < M; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--;
    v--;
    edge_map[make_pair(u, v)] = i;
    edge_map[make_pair(v, u)] = i;
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }

  ll mi = *min_element(D.begin(), D.end());
  if (count(D.begin(), D.end(), mi) < 2) {
    cout << -1 << endl;
    return 0;
  }

  vector<bool> used(N);
  vector<ll> color(N, -1), ans(M, -1);
  priority_queue<int, vector<int>, function<bool(int, int)>> que(
      [&](int u, int v) { return D[u] > D[v]; });

  UnionFind uf(N);

  bool cond = true;
  queue<int> q;
  for (int i = 0; i < N; i++) {
    if (D[i] != mi)
      continue;
    que.emplace(i);
    int cnt = 0;
    for (auto to : G[i]) {
      cnt += (D[to] == mi);
      if (D[to] == mi)
        uf.unite(i, to);
    }
    cond &= (cnt > 0);
  }

  if (!cond) {
    cout << -1 << endl;
    return 0;
  }

  vector<int> rec(N);
  for (int i = 0; i < N; i++) {
    if (D[i] != mi)
      continue;
    if (rec[uf.find(i)])
      continue;
    rec[uf.find(i)] = true;
    q.emplace(i);
  }
  while (q.size()) {
    int cur = q.front();
    q.pop();
    if (color[cur] < 0)
      color[cur] = 0;
    for (auto to : G[cur]) {
      if (color[to] >= 0)
        continue;
      color[to] = color[cur] ^ 1;
      q.emplace(to);
    }
  }

  while (que.size()) {
    int cur = que.top();
    que.pop();
    for (auto to : G[cur]) {
      if (color[to] >= 0) {
        ans[edge_map[make_pair(cur, to)]] = max(D[cur], D[to]);
      } else {
        ans[edge_map[make_pair(cur, to)]] = max(D[cur], D[to]);
        color[to] = color[cur] ^ 1;
        if (!used[to]) {
          used[to] = true;
          que.emplace(to);
        }
      }
    }
  }

  string color_str(N, '?');
  for (int i = 0; i < N; i++) {
    if (color[i] == 0)
      color_str[i] = 'B';
    else if (color[i] == 1)
      color_str[i] = 'W';
    else
      assert(false);
    // cerr << color_str[i];
  }
  // cerr << endl;

  for (int i = 0; i < M; i++) {
    assert(ans[i] > 0);
    // cerr << ans[i] << endl;
  }

  bool ok = true;
  for (int i = 0; i < N; i++) {
    ll min_val = LONGINF, cur = i;
    for (auto to : G[cur]) {
      if (color[cur] == color[to])
        continue;
      chmin<ll>(min_val, ans[edge_map[make_pair(cur, to)]]);
    }
    ok &= (D[i] == min_val);
  }

  if (!ok) {
    cout << -1 << endl;
    return 0;
  }

  cout << color_str << endl;
  for (int i = 0; i < M; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}


Fixed submission:
// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
using int64 = long long int;

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

// Union-Find 木 (Verified: AtCoder Typical Contest 001 B)
struct UnionFind {
private:
  const int n;
  int size_;
  vector<int> uf;

public:
  // 初期化 UnionFind uni(n) のように宣言すれば良い
  UnionFind(int _n) : n(_n), size_(_n), uf(_n, -1) {}
  // find (木の根を求める)
  int find(int x) { return (uf[x] < 0) ? x : uf[x] = find(uf[x]); }
  // x と y が同じ集合に属するかどうか
  bool same(int x, int y) { return find(x) == find(y); }
  // x が属する集合の要素数
  int size(int x) { return -uf[find(x)]; }
  // 集合はいくつあるか
  int size() { return size_; }
  // x と y の属する集合を併合
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    size_--;
    if (-uf[x] < -uf[y])
      swap(x, y);
    uf[x] += uf[y];
    uf[y] = x;
    return true;
  }
};

int main() {
  int N, M;
  scanf("%d%d", &N, &M);
  vector<ll> D(N);
  for (int i = 0; i < N; i++)
    scanf("%lld", &D[i]);

  vector<vector<int>> G(N);
  map<pair<int, int>, ll> edge_map;
  for (int i = 0; i < M; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--;
    v--;
    edge_map[make_pair(u, v)] = i;
    edge_map[make_pair(v, u)] = i;
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }

  ll mi = *min_element(D.begin(), D.end());
  if (count(D.begin(), D.end(), mi) < 2) {
    cout << -1 << endl;
    return 0;
  }

  vector<bool> used(N);
  vector<ll> color(N, -1), ans(M, -1);
  priority_queue<int, vector<int>, function<bool(int, int)>> que(
      [&](int u, int v) { return D[u] > D[v]; });

  UnionFind uf(N);

  bool cond = true;
  queue<int> q;
  for (int i = 0; i < N; i++) {
    if (D[i] != mi)
      continue;
    que.emplace(i);
    int cnt = 0;
    for (auto to : G[i]) {
      cnt += (D[to] == mi);
      if (D[to] == mi)
        uf.unite(i, to);
    }
    cond &= (cnt > 0);
  }

  if (!cond) {
    cout << -1 << endl;
    return 0;
  }

  vector<int> rec(N);
  for (int i = 0; i < N; i++) {
    if (D[i] != mi)
      continue;
    if (rec[uf.find(i)])
      continue;
    rec[uf.find(i)] = true;
    q.emplace(i);
  }
  while (q.size()) {
    int cur = q.front();
    q.pop();
    if (color[cur] < 0)
      color[cur] = 0;
    for (auto to : G[cur]) {
      if (color[to] >= 0 or D[to] != mi)
        continue;
      color[to] = color[cur] ^ 1;
      q.emplace(to);
    }
  }

  while (que.size()) {
    int cur = que.top();
    que.pop();
    for (auto to : G[cur]) {
      if (color[to] >= 0) {
        ans[edge_map[make_pair(cur, to)]] = max(D[cur], D[to]);
      } else {
        ans[edge_map[make_pair(cur, to)]] = max(D[cur], D[to]);
        color[to] = color[cur] ^ 1;
        if (!used[to]) {
          used[to] = true;
          que.emplace(to);
        }
      }
    }
  }

  string color_str(N, '?');
  for (int i = 0; i < N; i++) {
    if (color[i] == 0)
      color_str[i] = 'B';
    else if (color[i] == 1)
      color_str[i] = 'W';
    else
      assert(false);
    // cerr << color_str[i];
  }
  // cerr << endl;

  for (int i = 0; i < M; i++) {
    assert(ans[i] > 0);
    // cerr << ans[i] << endl;
  }

  bool ok = true;
  for (int i = 0; i < N; i++) {
    ll min_val = LONGINF, cur = i;
    for (auto to : G[cur]) {
      if (color[cur] == color[to])
        continue;
      chmin<ll>(min_val, ans[edge_map[make_pair(cur, to)]]);
    }
    ok &= (D[i] == min_val);
  }

  if (!ok) {
    cout << -1 << endl;
    return 0;
  }

  cout << color_str << endl;
  for (int i = 0; i < M; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}