QOJ.ac

QOJ

実行時間制限: 3 s メモリ制限: 512 MB 満点: 100

#1869. 当代艺术博物馆

統計

作为一名现代艺术爱好者,Nocriz 热衷于参观上海当代艺术博物馆(Power Station of Art)。

目前,博物馆正在展出一种名为“红黑图”的现代艺术作品。每一件“红黑图”艺术品都是一个无向带标号图,每个顶点都关联着一个数字和一种颜色。每个顶点要么是红色的,要么是黑色的。

可以通过以下方式修改图:选择一条边,交换该边所连接的两个顶点上的数字。此外,如果这两个顶点的颜色相同,则这两个顶点的颜色都会改变(从红色变为黑色,或从黑色变为红色)。否则,这两个顶点的颜色保持不变。

现在,Nocriz 正在研究两件艺术品。这两件艺术品的图结构相同,但顶点上的数字和颜色可能不同。请问是否可以通过若干次(可能为零次)修改,使这两件艺术品变得完全相同?

输入格式

第一行包含一个整数 $T$ ($1 \le T \le 3 \cdot 10^4$),表示测试用例的数量。接下来是 $T$ 个测试用例。

每个测试用例的第一行包含两个整数 $n$ 和 $m$ ($1 \le n \le 10^6, 0 \le m \le 10^6$),分别表示顶点数和边数。

接下来 $m$ 行,每行包含两个整数 $u_i$ 和 $v_i$ ($1 \le u_i, v_i \le n, u_i \neq v_i$),表示一条边。题目保证输入中没有重边,且图可能是不连通的。

随后是两组图的数字和颜色信息。对于每一组图: 第一行包含 $n$ 个整数,第 $i$ 个整数 $a_i$ ($0 \le a_i \le 10^6$) 表示第 $i$ 个顶点上的数字。 第二行包含 $n$ 个字符。如果第 $i$ 个字符为 ‘R’,则第 $i$ 个顶点为红色;如果第 $i$ 个字符为 ‘B’,则第 $i$ 个顶点为黑色。

题目保证 $\sum n \le 10^6$ 且 $\sum m \le 10^6$。

输出格式

对于每个测试用例,如果可以通过若干次(可能为零次)修改使两件艺术品变得相同,则输出一行 “YES”,否则输出 “NO”。

样例

输入 1

3
2 1
1 2
3 4
RR
4 3
BB
3 2
1 2
2 3
1 1 1
RBR
1 1 1
BBB
3 3
1 2
2 3
3 1
1 1 1
RBR
1 1 1
BBB

输出 1

YES
NO
YES

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.