Problem p02799 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()

#define LOCAL
#ifdef LOCAL
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define dump(x) true
#endif

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

template <class T, class U> void chmin(T &t, const U &u) {
  if (t > u)
    t = u;
}
template <class T, class U> void chmax(T &t, const U &u) {
  if (t < u)
    t = u;
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}

const int maxn = 100010;
const int L = TEN(9);

V<pii> g[maxn];

int main() {
  int N, M;
  cin >> N >> M;
  V<int> D(N);
  rep(i, N) cin >> D[i];
  V<pii> es(M);

  rep(i, M) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].eb(b, i);
    g[b].eb(a, i);
  }

  V<pii> vec;
  rep(i, N) vec.eb(mp(D[i], i));
  sort(ALL(vec));

  V<int> ans(M, L);
  V<int> col(N, -1);

  rep(i, N) {
    int v = vec[i].se;
    dump(v);
    if (col[v] != -1) {
      continue;
    }

    bool fd = 0;

    for (auto e : g[v]) {
      if (col[e.fi] != -1) {
        if (D[e.fi] == D[v]) {
          col[v] = col[e.fi] ^ 1;
          dump(mp(v, e.fi));
          dump(D[v]);
          ans[e.se] = D[v];
        } else {
          col[v] = col[e.fi];
          ans[e.se] = D[v] - D[e.fi];
          dump(mp(v, e.fi));
          dump(D[v] - D[e.fi]);
        }
        fd = 1;
        break;
      } else {
        if (D[v] == D[e.fi]) {
          col[e.fi] = 0;
          col[v] = 1;
          ans[e.se] = D[v];
          dump(e.se);
          dump(D[v]);
          fd = 1;
          break;
        }
      }
    }
    if (!fd) {
      puts("-1");
      return 0;
    }
    dump(ans);
  }

  string s(N, '?');
  rep(i, N) {
    if (col[i] == 1)
      s[i] = 'B';
    else
      s[i] = 'W';
  }
  cout << s << endl;

  rep(i, M) { cout << ans[i] << "\n"; }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()

#ifdef LOCAL
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define dump(x) true
#endif

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

template <class T, class U> void chmin(T &t, const U &u) {
  if (t > u)
    t = u;
}
template <class T, class U> void chmax(T &t, const U &u) {
  if (t < u)
    t = u;
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}

const int maxn = 100010;
const int L = TEN(9);

V<pii> g[maxn];

int main() {
  int N, M;
  cin >> N >> M;
  V<int> D(N);
  rep(i, N) cin >> D[i];
  V<pii> es(M);

  rep(i, M) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    g[a].eb(b, i);
    g[b].eb(a, i);
  }

  V<pii> vec;
  rep(i, N) vec.eb(mp(D[i], i));
  sort(ALL(vec));

  V<int> ans(M, L);
  V<int> col(N, -1);

  rep(i, N) {
    int v = vec[i].se;
    dump(v);
    if (col[v] != -1) {
      continue;
    }

    bool fd = 0;

    for (auto e : g[v]) {
      if (col[e.fi] != -1) {
        if (D[e.fi] == D[v]) {
          col[v] = col[e.fi] ^ 1;
          dump(mp(v, e.fi));
          dump(D[v]);
          ans[e.se] = D[v];
        } else {
          col[v] = col[e.fi];
          ans[e.se] = D[v] - D[e.fi];
          dump(mp(v, e.fi));
          dump(D[v] - D[e.fi]);
        }
        fd = 1;
        break;
      } else {
        if (D[v] == D[e.fi]) {
          col[e.fi] = 0;
          col[v] = 1;
          ans[e.se] = D[v];
          dump(e.se);
          dump(D[v]);
          fd = 1;
          break;
        }
      }
    }
    if (!fd) {
      puts("-1");
      return 0;
    }
    dump(ans);
  }

  string s(N, '?');
  rep(i, N) {
    if (col[i] == 1)
      s[i] = 'B';
    else
      s[i] = 'W';
  }
  cout << s << endl;

  rep(i, M) { cout << ans[i] << "\n"; }

  return 0;
}