Problem p02799 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(a) int(a.size())
const int N = 1e5 + 10;

int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}

int n, to[N << 1], nxt[N << 1], d[N], ww[N << 1], tt = 1, col[N], h[N], m;
bool vis[N];

void adde(int u, int v) {
  to[++tt] = v;
  nxt[tt] = h[u];
  h[u] = tt;
  to[++tt] = u;
  nxt[tt] = h[v];
  h[v] = tt;
}

void dfs(int u, int c = 0) {
  col[u] = c;
  vis[u] = 1;
  for (int i = h[u], v; v = to[i], i; i = nxt[i])
    if (~ww[i] && !vis[v])
      dfs(v, c ^ 1);
}

int main() {
  memset(ww, -1, sizeof(ww));
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    d[i] = gi();
  for (int i = 1, u, v; i <= m; i++)
    u = gi(), v = gi(), adde(u, v);
  for (int u = 1; u <= n; u++) {
    pii mn = mp(2e9, 0);
    int e = 0;
    for (int i = h[u], v; v = to[i], i; i = nxt[i])
      if (mp(d[v], v) < mn)
        mn = mp(d[v], v), e = i >> 1;
    if (mn.fi > d[u])
      return cout << -1, 0;
    if (e)
      ww[e << 1] = ww[e << 1 | 1] = d[u];
  }
  for (int i = 1; i <= n; i++)
    if (!vis[i])
      dfs(i);
  for (int i = 1; i <= n; i++)
    putchar(col[i] ? 'W' : 'B');
  cout << '\n';
  for (int i = 1; i <= m; i++) {
    if (!~ww[i << 1])
      ww[i << 1] = 1e9;
    cout << ww[i << 1] << '\n';
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(a) int(a.size())
const int N = 2e5 + 10;

int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}

int n, to[N << 1], nxt[N << 1], d[N], ww[N << 1], tt = 1, col[N], h[N], m;
bool vis[N];

void adde(int u, int v) {
  to[++tt] = v;
  nxt[tt] = h[u];
  h[u] = tt;
  to[++tt] = u;
  nxt[tt] = h[v];
  h[v] = tt;
}

void dfs(int u, int c = 0) {
  col[u] = c;
  vis[u] = 1;
  for (int i = h[u], v; v = to[i], i; i = nxt[i])
    if (~ww[i] && !vis[v])
      dfs(v, c ^ 1);
}

int main() {
  memset(ww, -1, sizeof(ww));
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    d[i] = gi();
  for (int i = 1, u, v; i <= m; i++)
    u = gi(), v = gi(), adde(u, v);
  for (int u = 1; u <= n; u++) {
    pii mn = mp(2e9, 0);
    int e = 0;
    for (int i = h[u], v; v = to[i], i; i = nxt[i])
      if (mp(d[v], v) < mn)
        mn = mp(d[v], v), e = i >> 1;
    if (mn.fi > d[u])
      return cout << -1, 0;
    if (e)
      ww[e << 1] = ww[e << 1 | 1] = d[u];
  }
  for (int i = 1; i <= n; i++)
    if (!vis[i])
      dfs(i);
  for (int i = 1; i <= n; i++)
    putchar(col[i] ? 'W' : 'B');
  cout << '\n';
  for (int i = 1; i <= m; i++) {
    if (!~ww[i << 1])
      ww[i << 1] = 1e9;
    cout << ww[i << 1] << '\n';
  }
  return 0;
}