QOJ.ac

QOJ

시간 제한: 6.0 s 메모리 제한: 1024 MB 총점: 100 해킹 가능 ✓

#17155. Fix the Coloring

통계

给你一个含有 $n$ 个顶点和 $m$ 条边的无向图。每个顶点都被染成了黑色或白色。你希望这种染色是合理的(即,任何由边相连的两个顶点都不能具有相同的颜色)。

可能会有一些顶点对不满足这个性质。你想通过重新给一些顶点染色来解决这个问题。然而,你只有红色油漆,所以你只能将一些顶点重新染成红色。

但即使手头有三种颜色,修复染色也可能是无法实现的。例如,当图中包含一个大团(即一个顶点集合,其中任意两个顶点之间都有边相连)时。你决定允许该图中至多一个非平凡团的所有顶点都被染成红色。

因此,你的任务是检查是否可以通过将一些顶点染成红色,使得:

  • 所有连接红色顶点的边构成一个团。
  • 所有其余的边都连接不同颜色的顶点。

输入格式

第一行包含一个整数 $t$ ($1 \le t \le 1000$),表示测试用例的数量。

每个测试用例的第一行包含两个整数 $n$ 和 $m$ ($1 \le n, m \le 3000$),分别表示图中的顶点数和边数。顶点编号为 $1$ 到 $n$。

第二行包含一个长度为 $n$ 的由字符 BW 组成的字符串;第 $i$ 个字符表示第 $i$ 个顶点的颜色(B 表示黑色,W 表示白色)。

接下来的 $m$ 行包含图的边,每行由一对顶点编号 $a_i, b_i$ ($1 \le a_i, b_i \le n, a_i \ne b_i$) 描述。每个无序对 $u, v$ 在每个测试用例中最多出现一次。

所有测试用例中 $n$ 的总和以及 $m$ 的总和均不超过 $3000$。

输出格式

输出所有 $t$ 个测试用例的答案。

如果无法修复染色,则输出一行,包含单词 NO。否则,答案应包含两行:第一行包含一个单词 YES,第二行包含一个长度为 $n$ 的由 BWR 组成的字符串,表示顶点的新颜色(其中 R 表示红色)。如果存在多个有效解,你可以输出其中任意一个。

样例

输入 1

2
6 7
BBBWBB
1 2
2 3
3 1
2 4
2 5
4 5
5 6
6 7
BBBBBB
1 2
2 3
3 1
2 4
2 5
4 5
5 6

输出 1

YES
RRRWBR
NO

说明

在第一个测试用例中,一种解法是将团 $1, 2, 3$ 以及顶点 $6$ 重新染色。另一种解法是将团 $1, 3$ 以及顶点 $5$ 重新染色。

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#1054EditorialOpen题解jiangly2026-02-19 13:12:46View

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.