Problem p02799 (C++)

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

using namespace std;

using ll = long long;

mt19937 mt(736);

class dsu {
  vector<int> par, siz;

public:
  dsu(int n) : par(n, -1), siz(n, 1) {}

  int get_par(int v) { return par[v] == -1 ? v : get_par(par[v]); }

  bool unite(int a, int b) {
    a = get_par(a);
    b = get_par(b);

    if (a == b)
      return false;

    if (siz[a] < siz[b])
      swap(a, b);

    par[b] = a;
    siz[a] += siz[b];

    return true;
  }
};

void dfs(const vector<vector<pair<int, int>>> &gr, vector<bool> &used,
         vector<bool> &col, const unordered_set<int> &rev, int v, bool c) {
  if (used[v])
    return;
  used[v] = true;
  col[v] = c;

  for (auto it : gr[v]) {
    auto nc = c;

    if (rev.count(it.second))
      nc = !nc;

    dfs(gr, used, col, rev, it.first, nc);
  }
}

void solve(istream &cin = std::cin, ostream &cout = std::cout) {
  int n, m;

  cin >> n >> m;

  vector<int> d(n);

  for (auto &it : d)
    cin >> it;

  vector<pair<int, int>> edges;

  for (int i = 0; i < m; i++) {
    int a, b;

    cin >> a >> b;
    a--;
    b--;

    edges.emplace_back(a, b);
  }

  vector<pair<int, int>> best(n, {-1, -1});
  vector<int> c(m, (int)numeric_limits<int>::max());

  for (int i = 0; i < m; i++) {
    int a, b;

    tie(a, b) = edges[i];

    if (best[a].first == -1 ||
        pair<int, int>{d[b], i} <
            pair<int, int>{d[best[a].first], best[a].second})
      best[a] = {b, i};
    if (best[b].first == -1 ||
        pair<int, int>{d[a], i} <
            pair<int, int>{d[best[b].first], best[b].second})
      best[b] = {a, i};
  }

  if (find(best.begin(), best.end(), pair<int, int>{-1, -1}) != best.end()) {
    cout << -1 << endl;

    return;
  }

  for (int i = 0; i < n; i++)
    if (d[best[i].first] > d[i]) {
      cout << -1 << endl;
      cerr << "bad: " << i << endl;

      return;
    }

  unordered_set<int> rev;
  vector<vector<pair<int, int>>> gr(n);

  for (auto &e : best) {
    c[e.second] = abs(d[edges[e.second].first] - d[edges[e.second].second]);

    if (c[e.second] == 0) {
      c[e.second] = d[edges[e.second].first];
      rev.insert(e.second);
    }

    gr[e.first].emplace_back(e.second, e.second);
    gr[e.second].emplace_back(e.first, e.second);
  }

  vector<bool> used(n), col(n);

  for (int i = 0; i < n; i++)
    dfs(gr, used, col, rev, i, false);

  assert(count(col.begin(), col.end(), false));
  assert(count(col.begin(), col.end(), true));

  for (auto it : col)
    cout << (it ? 'B' : 'W');
  cout << endl;

  for (auto &it : c) {
    it = min(it, (int)1e9);

    cout << it << '\n';
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  cout << fixed;

#ifdef LOCAL
  auto st = clock();

  ifstream fin("../input.txt");

  solve(fin);

  cout << "clock: " << (clock() - st) / (double)CLOCKS_PER_SEC << endl;
#else
  solve();
#endif

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

mt19937 mt(736);

class dsu {
  vector<int> par, siz;

public:
  dsu(int n) : par(n, -1), siz(n, 1) {}

  int get_par(int v) { return par[v] == -1 ? v : get_par(par[v]); }

  bool unite(int a, int b) {
    a = get_par(a);
    b = get_par(b);

    if (a == b)
      return false;

    if (siz[a] < siz[b])
      swap(a, b);

    par[b] = a;
    siz[a] += siz[b];

    return true;
  }
};

void dfs(const vector<vector<pair<int, int>>> &gr, vector<bool> &used,
         vector<bool> &col, const unordered_set<int> &rev, int v, bool c) {
  if (used[v])
    return;
  used[v] = true;
  col[v] = c;

  for (auto it : gr[v]) {
    auto nc = c;

    if (rev.count(it.second))
      nc = !nc;

    dfs(gr, used, col, rev, it.first, nc);
  }
}

void solve(istream &cin = std::cin, ostream &cout = std::cout) {
  int n, m;

  cin >> n >> m;

  vector<int> d(n);

  for (auto &it : d)
    cin >> it;

  vector<pair<int, int>> edges;

  for (int i = 0; i < m; i++) {
    int a, b;

    cin >> a >> b;
    a--;
    b--;

    edges.emplace_back(a, b);
  }

  vector<pair<int, int>> best(n, {-1, -1});
  vector<int> c(m, (int)numeric_limits<int>::max());

  for (int i = 0; i < m; i++) {
    int a, b;

    tie(a, b) = edges[i];

    if (best[a].first == -1 ||
        pair<int, int>{d[b], i} <
            pair<int, int>{d[best[a].first], best[a].second})
      best[a] = {b, i};
    if (best[b].first == -1 ||
        pair<int, int>{d[a], i} <
            pair<int, int>{d[best[b].first], best[b].second})
      best[b] = {a, i};
  }

  if (find(best.begin(), best.end(), pair<int, int>{-1, -1}) != best.end()) {
    cout << -1 << endl;

    return;
  }

  for (int i = 0; i < n; i++)
    if (d[best[i].first] > d[i]) {
      cout << -1 << endl;
      cerr << "bad: " << i << endl;

      return;
    }

  unordered_set<int> rev;
  vector<vector<pair<int, int>>> gr(n);

  for (auto &e : best) {
    c[e.second] = abs(d[edges[e.second].first] - d[edges[e.second].second]);

    if (c[e.second] == 0) {
      c[e.second] = d[edges[e.second].first];
      rev.insert(e.second);
    }

    auto be = edges[e.second];

    gr[be.first].emplace_back(be.second, e.second);
    gr[be.second].emplace_back(be.first, e.second);
  }

  vector<bool> used(n), col(n);

  for (int i = 0; i < n; i++)
    dfs(gr, used, col, rev, i, false);

  assert(count(col.begin(), col.end(), false));
  assert(count(col.begin(), col.end(), true));

  for (auto it : col)
    cout << (it ? 'B' : 'W');
  cout << endl;

  for (auto &it : c) {
    it = min(it, (int)1e9);

    cout << it << '\n';
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  cout << fixed;

#ifdef LOCAL
  auto st = clock();

  ifstream fin("../input.txt");

  solve(fin);

  cout << "clock: " << (clock() - st) / (double)CLOCKS_PER_SEC << endl;
#else
  solve();
#endif

  return 0;
}