Language: C++

#include <algorithm>
#include <cassert>
#include <queue>
#include <vector>
#include <iostream>
using namespace std;

void dfs(const vector<vector<int>> &G, const map<pair<int,int>> &E,
         vector<int> &W, const vector<int> &D, string &C, int v){
  for(auto v_ : G[v]){
    if(C[v_] != '?') continue;
    if(D[v_] <= D[v]) continue;
    pair<int,int> e(v,v_);
    if(v > v_) swap(e.first,e.second);
    int idx = E[e];

    W[idx] = D[v_]-D[v];
    C[v_] = C[v];
    dfs(G,E,W,D,C,v_);
  }
}

int main(){
  const int INF = 1e9;
  int N, M;
  cin >> N >> M;
  vector<int> D(N);
  for(int i = 0; i < N; ++i){
    cin >> D[i];
  }
  map<pair<int,int>,int> E;
  vector<vector<int>> G(N);
  for(int i = 0; i < M; ++i){
    int u, v;
    cin >> u >> v;
    --u,--v;
    if(u > v) swap(u,v);
    E[{u,v}] = i;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  int d_min = *min_element(D.begin(), D.end());
  // if(count(D.begin(), D.end(), d_min) < 2){
  //   cout << -1 << endl;
  //   return 0;
  // }
  priority_queue<pair<int,int>> pq;
  for(int i = 0; i < N; ++i){
    pq.emplace(-D[i],i);
  }
  vector<int> W(M,-1);
  string C(N,'?');
  char c = 'W';
  while(!pq.empty()){
    int v = pq.top().second;
    int d = -pq.top().first;
    pq.pop();
    if(C[v] != '?') continue;
    bool f = false;
    for(auto v_ : G[v]){
      if(D[v_] == D[v])
        f = true;
    }
    if(!f){
      cout << -1 << endl;
      return 0;
    }
    D[v] = 0;
    C[v] = c;
    dfs(G,E,W,D,C,v);
    if(c == 'W') c = 'B';
    else c = 'W';
    C[v] = c;
  }
  for(auto &w : W){
    if(w < 0) w = INF;
  }
  cout << C << endl;
  for(auto w : W){
    cout << w << "\n";
  }
}


Compiler output:
a.cc:8:46: error: 'map' does not name a type
    8 | void dfs(const vector<vector<int>> &G, const map<pair<int,int>> &E,
      |                                              ^~~
a.cc:8:49: error: expected ',' or '...' before '<' token
    8 | void dfs(const vector<vector<int>> &G, const map<pair<int,int>> &E,
      |                                                 ^
a.cc: In function 'void dfs(const std::vector<std::vector<int> >&, int)':
a.cc:10:19: error: 'v' was not declared in this scope; did you mean 'v_'?
   10 |   for(auto v_ : G[v]){
      |                   ^
      |                   v_
a.cc:11:8: error: 'C' was not declared in this scope
   11 |     if(C[v_] != '?') continue;
      |        ^
a.cc:12:8: error: 'D' was not declared in this scope
   12 |     if(D[v_] <= D[v]) continue;
      |        ^
a.cc:15:15: error: 'E' was not declared in this scope
   15 |     int idx = E[e];
      |               ^
a.cc:17:5: error: 'W' was not declared in this scope
   17 |     W[idx] = D[v_]-D[v];
      |     ^
a.cc:17:14: error: 'D' was not declared in this scope
   17 |     W[idx] = D[v_]-D[v];
      |              ^
a.cc:18:5: error: 'C' was not declared in this scope
   18 |     C[v_] = C[v];
      |     ^
a.cc: In function 'int main()':
a.cc:31:3: error: 'map' was not declared in this scope
   31 |   map<pair<int,int>,int> E;
      |   ^~~
a.cc:6:1: note: 'std::map' is defined in header '<map>'; this is probably fixable by adding '#include <map>'
    5 | #include <iostream>
  +++ |+#include <map>
    6 | using namespace std;
a.cc:31:20: error: expected primary-expression before ',' token
   31 |   map<pair<int,int>,int> E;
      |                    ^
a.cc:31:21: error: expected primary-expression before 'int'
   31 |   map<pair<int,int>,int> E;
      |                     ^~~
a.cc:38:5: error: 'E' was not declared in this scope
   38 |     E[{u,v}] = i;
      |     ^
a.cc:70:11: error: 'E' was not declared in this scope
   70 |     dfs(G,E,W,D,C,v);
      |           ^