atcoder#KEYENCE2020E. Bichromization
Bichromization
Score : points
Problem Statement
We have a connected undirected graph with vertices and edges. Edge in this graph () connects Vertex and Vertex bidirectionally. We are additionally given integers .
Determine whether the conditions below can be satisfied by assigning a color - white or black - to each vertex and an integer weight between and (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 (), the following holds.- The minimum cost to travel from Vertex to a vertex whose color assigned is different from that of Vertex by traversing the edges is equal to .
- The minimum cost to travel from Vertex to a vertex whose color assigned is different from that of Vertex by traversing the edges is equal to .
Here, the cost of traversing the edges is the sum of the weights of the edges traversed.
Constraints
- 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:
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:
Here,
- the first line should contain the string of length . Its -th character () should be
W
if Vertex is assigned white andB
if it is assigned black. - The -th line () should contain the integer weight assigned to Edge .
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 , for example. To travel from Vertex , which is assigned black, to a vertex that is assigned white with the minimum cost, we should make these moves: Vertex Vertex Vertex . The total cost of these moves is , 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