atcoder#KEYENCE2020E. Bichromization

Bichromization

Score : 900900 points

Problem Statement

We have a connected undirected graph with NN vertices and MM edges. Edge ii in this graph (1iM1 \leq i \leq M) connects Vertex UiU_i and Vertex ViV_i bidirectionally. We are additionally given NN integers D1,D2,...,DND_1, D_2, ..., D_N.

Determine whether the conditions below can be satisfied by assigning a color - white or black - to each vertex and an integer weight between 11 and 10910^9 (inclusive) to each edge in this graph. If the answer is yes, find one such assignment of colors and integers, too.

  • There is at least one vertex assigned white and at least one vertex assigned black.
  • For each vertex vv (1vN1 \leq v \leq N), the following holds.- The minimum cost to travel from Vertex vv to a vertex whose color assigned is different from that of Vertex vv by traversing the edges is equal to DvD_v.
  • The minimum cost to travel from Vertex vv to a vertex whose color assigned is different from that of Vertex vv by traversing the edges is equal to DvD_v.

Here, the cost of traversing the edges is the sum of the weights of the edges traversed.

Constraints

  • 2N100,0002 \leq N \leq 100,000
  • 1M200,0001 \leq M \leq 200,000
  • 1Di1091 \leq D_i \leq 10^9
  • 1Ui,ViN1 \leq U_i, V_i \leq N
  • The given graph is connected and has no self-loops or multiple edges.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM

D1D_1 D2D_2 ...... DND_N

U1U_1 V1V_1

U2U_2 V2V_2

\vdots

UMU_M VMV_M

Output

If there is no assignment satisfying the conditions, print a single line containing -1.

If such an assignment exists, print one such assignment in the following format:

SS

C1C_1

C2C_2

\vdots

CMC_M

Here,

  • the first line should contain the string SS of length NN. Its ii-th character (1iN1 \leq i \leq N) should be W if Vertex ii is assigned white and B if it is assigned black.
  • The (i+1)(i + 1)-th line (1iM1 \leq i \leq M) should contain the integer weight CiC_i assigned to Edge ii.
5 5
3 4 3 5 7
1 2
1 3
3 2
4 2
4 5
BWWBB
4
3
1
5
2

Assume that we assign the colors and integers as the sample output, and let us consider Vertex 55, for example. To travel from Vertex 55, which is assigned black, to a vertex that is assigned white with the minimum cost, we should make these moves: Vertex 55 \to Vertex 44 \to Vertex 22. The total cost of these moves is 77, which satisfies the condition. We can also verify that the condition is satisfied for other vertices.

5 7
1 2 3 4 5
1 2
1 3
1 4
2 3
2 5
3 5
4 5
-1
4 6
1 1 1 1
1 2
1 3
1 4
2 3
2 4
3 4
BBBW
1
1
1
2
1
1