CF1288F.Red-Blue Graph

省选/NOI-

通过率:0%

时间限制:1.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a bipartite graph: the first part of this graph contains n1n_1 vertices, the second part contains n2n_2 vertices, and there are mm 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 rr coins) or paint it blue (it costs bb 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.

给你一个二分图:该图的第一部分包含 n1n_1 个顶点,第二部分包含 n2n_2 个顶点,共有 mm 条边。图中可能含有多重边。

初始时,每条边均无颜色。对于每条边,你可以选择:保持其无色(即免费),将其涂成红色(花费 rr 枚硬币),或将其涂成蓝色(花费 bb 枚硬币)。任意一条边不能同时被涂成红色和蓝色。

图中的顶点分为三类:无色顶点、红色顶点和蓝色顶点。带颜色的顶点对关联边的颜色施加额外约束:

  • 对每个红色顶点,与其关联的红色边的数量必须严格大于与其关联的蓝色边的数量;
  • 对每个蓝色顶点,与其关联的蓝色边的数量必须严格大于与其关联的红色边的数量。

无色顶点不施加任何额外约束。

你的目标是给某些(可能为零条)边染色,使得所有约束均被满足;在所有满足约束的方案中,你应选择总花费最小的一种。

输入格式

The first line contains five integers n1n_1, n2n_2, mm, rr and bb (1≤n1,n2,m,r,b≤2001 \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 n1n_1 characters. Each character is either U, R or B. If the ii-th character is U, then the ii-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 n2n_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 mm lines follow, the ii-th line contains two integers uiu_i and viv_i (1≤ui≤n11 \le u_i \le n_1, 1≤vi≤n21 \le v_i \le n_2) denoting an edge connecting the vertex uiu_i from the first part and the vertex viv_i from the second part.

The graph may contain multiple edges.

第一行包含五个整数 n1n_1、n2n_2、mm、rr 和 bb(1≤n1,n2,m,r,b≤2001 \le n_1, n_2, m, r, b \le 200),分别表示二分图第一部分的顶点数、第二部分的顶点数、边数、将一条边染成红色所需支付的金币数,以及将一条边染成蓝色所需支付的金币数。

第二行包含一个由 n1n_1 个字符组成的字符串。每个字符为 U、R 或 B。若第 ii 个字符为 U,则第一部分的第 ii 个顶点未染色;R 表示该顶点为红色,B 表示该顶点为蓝色。

第三行包含一个由 n2n_2 个字符组成的字符串。每个字符为 U、R 或 B。该字符串以相同方式表示第二部分各顶点的颜色。

接下来是 mm 行,其中第 ii 行包含两个整数 uiu_i 和 viv_i(1≤ui≤n11 \le u_i \le n_1,1≤vi≤n21 \le v_i \le n_2),表示连接第一部分中顶点 uiu_i 与第二部分中顶点 viv_i 的一条边。

该图可能包含重边。

输出格式

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

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

如果不存在满足所有约束条件的染色方案,则输出一个整数 −1-1。

否则,输出一个整数 cc,表示染色的总代价,以及一个长度为 mm 的字符串。该字符串的第 ii 个字符应为:U(表示第 ii 条边不染色)、R(表示第 ii 条边染红色)或 B(表示第 ii 条边染蓝色)。若存在多种具有最小可能代价的染色方案,输出任意一种即可。

输入输出样例

  • 输入#1

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

    输出#1

    35
    BUURRU
  • 输入#2

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

    输出#2

    -1
  • 输入#3

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

    输出#3

    14
    RBB

输入解题思路,AI测评打分。不知道怎么写?

首页