#x1073. CFP1288F Red-Blue Graph

CFP1288F Red-Blue Graph

Red-Blue Graph

题面翻译

有一张二分图,左边有 n1n_1 个点,右边有 n2n_2 个点,mm 条边。每个点可能有一种颜色 R 或者 B,也可能没有,也就是 U。现在要给一些边染色,把边染成 R 要花费 rr 的代价,把边染成 B 要花费 bb 的代价,要求对于每个颜色为 R 的点,与之相邻的边中 R 的边严格多于 B 的边;对于每个颜色为 B 的点,与之相邻的边中 B 的边严格多于 R 的边。求花费最小的方案,输出任意一种,无解输出 1-1。其中 1n1,n2,m,r,b2001 \le n_1, n_2, m, r, b \le 200

题目描述

You are given a bipartite graph: the first part of this graph contains n1 n_1 vertices, the second part contains n2 n_2 vertices, and there are m m edges. The graph can contain multiple edges.

Initially, each edge is colorless. For each edge, you may either leave it uncolored (it is free), paint it red (it costs r r coins) or paint it blue (it costs b b coins). No edge can be painted red and blue simultaneously.

There are three types of vertices in this graph — colorless, red and blue. Colored vertices impose additional constraints on edges' colours:

  • for each red vertex, the number of red edges indicent to it should be strictly greater than the number of blue edges incident to it;
  • for each blue vertex, the number of blue edges indicent to it should be strictly greater than the number of red edges incident to it.

Colorless vertices impose no additional constraints.

Your goal is to paint some (possibly none) edges so that all constraints are met, and among all ways to do so, you should choose the one with minimum total cost.

输入格式

The first line contains five integers n1 n_1 , n2 n_2 , m m , r r and b b ( 1n1,n2,m,r,b200 1 \le n_1, n_2, m, r, b \le 200 ) — the number of vertices in the first part, the number of vertices in the second part, the number of edges, the amount of coins you have to pay to paint an edge red, and the amount of coins you have to pay to paint an edge blue, respectively.

The second line contains one string consisting of n1 n_1 characters. Each character is either U, R or B. If the i i -th character is U, then the i i -th vertex of the first part is uncolored; R corresponds to a red vertex, and B corresponds to a blue vertex.

The third line contains one string consisting of n2 n_2 characters. Each character is either U, R or B. This string represents the colors of vertices of the second part in the same way.

Then m m lines follow, the i i -th line contains two integers ui u_i and vi v_i ( 1uin1 1 \le u_i \le n_1 , 1vin2 1 \le v_i \le n_2 ) denoting an edge connecting the vertex ui u_i from the first part and the vertex vi v_i from the second part.

The graph may contain multiple edges.

输出格式

If there is no coloring that meets all the constraints, print one integer 1 -1 .

Otherwise, print an integer c c denoting the total cost of coloring, and a string consisting of m m characters. The i i -th character should be U if the i i -th edge should be left uncolored, R if the i i -th edge should be painted red, or B if the i i -th edge should be painted blue. If there are multiple colorings with minimum possible cost, print any of them.

样例 #1

样例输入 #1

3 2 6 10 15
RRB
UB
3 2
2 2
1 2
1 1
2 1
1 1

样例输出 #1

35
BUURRU

样例 #2

样例输入 #2

3 1 3 4 5
RRR
B
2 1
1 1
3 1

样例输出 #2

-1

样例 #3

样例输入 #3

3 1 3 4 5
URU
B
2 1
1 1
3 1

样例输出 #3

14
RBB