Problem p02799 (C++)

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

using namespace std;

const int MAXN = 100010;

int n, m;
int a[MAXN];
vector<int> g[MAXN];
pair<int, int> edges[MAXN];

string ansS;
map<pair<int, int>, int> ansC;

int visited[MAXN];

int dfs(int v, int cost, int color) {
  ansS[v] = (color ? 'B' : 'W');
  visited[v] = 1;
  int found = 0;
  for (int s : g[v])
    if (!visited[s] && a[s] == a[v]) {
      ansC[make_pair(s, v)] = ansC[make_pair(v, s)] = cost;
      dfs(s, cost, 1 - color);
      found = 1;
    }
  return found;
}

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

  vector<int> p;

  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    p.push_back(i);
  }

  ansS = string(n, '*');

  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    g[u].push_back(v);
    g[v].push_back(u);
    edges[i] = {u, v};
  }

  sort(p.begin(), p.end(), [&](int x, int y) { return a[x] < a[y]; });

  for (int i = 0; i < n; ++i) {
    int v = p[i];
    if (visited[v])
      continue;
    if (!dfs(v, a[v], 0)) {
      ansS[v] = '*';
      for (int s : g[v]) {
        if (ansS[s] != '*') {
          if (ansS[v] == '*') {
            ansS[v] = (ansS[s] == 'B' ? 'W' : 'B');
            ansC[make_pair(s, v)] = ansC[make_pair(v, s)] = a[v];
          } else {
            if (ansS[v] != ansS[s]) {
              ansC[make_pair(s, v)] = ansC[make_pair(v, s)] = a[v];
            } else {
              ansC[make_pair(s, v)] = ansC[make_pair(v, s)] = a[v] - a[s];
            }
          }
        }
      }
      if (ansS[v] == '*')
        return cout << -1, 0;
    }
  }

  cout << ansS << "\n";
  for (int i = 0; i < m; ++i) {
    //  cout << edges[i] << endl;
    if (!ansC.count(edges[i]))
      cout << (int)1e9 << "\n";
    else
      cout << ansC[edges[i]] << "\n";
  }

  return 0;
}


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

using namespace std;

const int MAXN = 200010;

int n, m;
int a[MAXN];
vector<int> g[MAXN];
pair<int, int> edges[MAXN];

string ansS;
map<pair<int, int>, int> ansC;

int visited[MAXN];

int dfs(int v, int cost, int color) {
  ansS[v] = (color ? 'B' : 'W');
  visited[v] = 1;
  int found = 0;
  for (int s : g[v])
    if (!visited[s] && a[s] == a[v]) {
      ansC[make_pair(s, v)] = ansC[make_pair(v, s)] = cost;
      dfs(s, cost, 1 - color);
      found = 1;
    }
  return found;
}

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

  vector<int> p;

  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    p.push_back(i);
  }

  ansS = string(n, '*');

  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    --u;
    --v;
    g[u].push_back(v);
    g[v].push_back(u);
    edges[i] = {u, v};
  }

  sort(p.begin(), p.end(), [&](int x, int y) { return a[x] < a[y]; });

  for (int i = 0; i < n; ++i) {
    int v = p[i];
    if (visited[v])
      continue;
    if (!dfs(v, a[v], 0)) {
      ansS[v] = '*';
      for (int s : g[v]) {
        if (ansS[s] != '*') {
          if (ansS[v] == '*') {
            ansS[v] = (ansS[s] == 'B' ? 'W' : 'B');
            ansC[make_pair(s, v)] = ansC[make_pair(v, s)] = a[v];
          } else {
            if (ansS[v] != ansS[s]) {
              ansC[make_pair(s, v)] = ansC[make_pair(v, s)] = a[v];
            } else {
              ansC[make_pair(s, v)] = ansC[make_pair(v, s)] = a[v] - a[s];
            }
          }
        }
      }
      if (ansS[v] == '*')
        return cout << -1, 0;
    }
  }

  cout << ansS << "\n";
  for (int i = 0; i < m; ++i) {
    //  cout << edges[i] << endl;
    if (!ansC.count(edges[i]))
      cout << (int)1e9 << "\n";
    else
      cout << ansC[edges[i]] << "\n";
  }

  return 0;
}