Language: C++

#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>

template <class T, class U>
inline bool chmin(T& lhs, const U& rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U>
inline bool chmax(T& lhs, const U& rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

// [l, r) from l to r
struct frep {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { ++i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr frep(int l_, int r_): l(std::min(l_, r_)), r(r_) {}
  constexpr itr begin() const { return l; }
  constexpr itr end() const { return r; }
};

// [l, r) from r to l
struct brep {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { --i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr r, l;
  constexpr brep(int r_, int l_): r(std::max(l_, r_) - 1), l(l_ - 1) {}
  constexpr itr begin() const { return r; }
  constexpr itr end() const { return l; }
};

template <class T>
inline T scan() {
  T res;
  std::cin >> res;
  return res;
}


int main() {
  constexpr int inf = 1000000000;
  int N, M;
  std::cin >> N >> M;
  std::vector<int> D(N);
  for (int& x : D) {
    std::cin >> x;
  }
  std::vector<std::vector<int>> graph(N);
  std::map<std::pair<int, int>, int> edge;
  for (const int i: frep(0, M)) {
    int v = scan<int>() - 1;
    int u = scan<int>() - 1;
    graph[v].emplace_back(u);
    graph[u].emplace_back(v);
    edge[std::minmax(u, v)] = i;
  }
  std::vector<int> answer(M, inf);
  std::vector<std::vector<int>> remain(N);
  for (const int v: frep(0, N)) {
    int val = inf, match = inf;
    for (const int u: graph[v]) {
      if (val > D[u]) {
        val = D[u];
        match = u;
      } else if (val == D[u]) {
        chmin(match, u);
      }
    }
    if (val > D[v]) {
      std::cout << "-1\n";
      return 0;
    }
    answer[edge[std::minmax(v, match)]] = D[v];
    remain[v].emplace_back(match);
    remain[match].emplace_back(v);
  }
  std::vector<int> color(N);
  std::function<void(int, int)> dfs = [&](int v, int c) {
    if (color[v] > 0) {
      return;
    }
    color[v] = c;
    for (const int u: remain[v]) {
      dfs(u, 3 - c);
    }
  };
  for (const int v: frep(0, N)) {
    dfs(v, 1);
    std::cout << (color[v] == 1 ? 'B' : 'W');
  }
  std::cout << '\n';
  for (const int i: frep(0, M)) {
    std::cout << answer[i] << '\n';
  }
  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:101:8: error: 'function' is not a member of 'std'
  101 |   std::function<void(int, int)> dfs = [&](int v, int c) {
      |        ^~~~~~~~
a.cc:6:1: note: 'std::function' is defined in header '<functional>'; this is probably fixable by adding '#include <functional>'
    5 | #include <map>
  +++ |+#include <functional>
    6 | 
a.cc:101:30: error: expression list treated as compound expression in functional cast [-fpermissive]
  101 |   std::function<void(int, int)> dfs = [&](int v, int c) {
      |                              ^
a.cc:101:17: error: expected primary-expression before 'void'
  101 |   std::function<void(int, int)> dfs = [&](int v, int c) {
      |                 ^~~~
a.cc:111:5: error: 'dfs' was not declared in this scope
  111 |     dfs(v, 1);
      |     ^~~