#P9189. [USACO23OPEN] Custodial Cleanup G

[USACO23OPEN] Custodial Cleanup G

题目描述

Due to the disorganized structure of his mootels (much like motels but with bovine rather than human guests), Farmer John has decided to take up the role of the mootel custodian to restore order to the stalls.

Each mootel has NN stalls labeled 11 through NN and MM corridors that connect pairs of stalls to each other bidirectionally. The ii th stall is painted with color CiC_i and initially has a single key of color SiS_i in it. FJ will have to rearrange the keys to appease the cows and restore order to the stalls.

FJ starts out in stall 11 without holding any keys and is allowed to repeatedly do one of the following moves:

  • Pick up a key in the stall he is currently in. FJ can hold multiple keys at a time.
  • Place down a key he is holding into the stall he is currently in. A stall may hold multiple keys at a time.
  • Enter stall 11 by moving through a corridor.
  • Enter a stall other than stall 11 by moving through a corridor. He can only do this if he currently holds a key that is the same color as the stall he is entering.

Unfortunately, it seems that the keys are not in their intended locations. To restore order to FJ's mootel, the iith stall requires that a single key of color FiF_i is in it. It is guaranteed that SS is a permutation of FF.

For TT different mootels, FJ starts in stall 11 and needs to place every key in its appropriate location, ending back in stall 11. For each of the TT mootels, please answer if it is possible to do this.

输入格式

The first line contains TT, the number of mootels (test cases).

Each test case will be preceded by a blank line. Then, the first line of each test case contains two integers NN and MM.

The second line of each test case contains NN integers. The ii-th integer on this line CiC_i means that stall ii has color CiC_i.

The third line of each test case contains NN integers. The ii-th integer on this line SiS_i means that stall ii initially holds a key of color SiS_i.

The fourth line of each test case contains NN integers. The ii-th integer on this line FiF_i means that stall ii needs to have a key of color FiF_i in it.

The next MM lines of each test case follow. The ii-th of these lines contains two distinct integers uiu_i and viv_i. This represents that a corridor exists between stalls uiu_i and viv_i. No corridors are repeated.

输出格式

For each mootel, output YES on a new line if there exists a way for FJ to return a key of color FiF_i to each stall ii and end back in stall 11. Otherwise, output NO on a new line.

题目大意

奶牛旅馆可以被看作一个 NN 个节点 MM 条边的无向简单图,其中每个房间有一个颜色 CiC_i,以及一个钥匙,颜色为 SiS_i, FJ 最初在 11 号节点,手上一把钥匙都没有。

FJ 可以进行无数次以下操作:

  • 捡起当前房间的钥匙。(FJ 可以同时手持多个钥匙)

  • 将部分或全部手上的钥匙放在当前房间。 (房间内可以同时放多把钥匙)

  • 通过一条边,移到一个相邻的房间,前提是目标房间是房间 11, 或者 FJ 拥有至少一个目标房间颜色的钥匙。

已知 FFSS 的排列, FJ 想要让每个房间里面都恰好有一个 FiF_i 颜色的钥匙,并且最后回到 11 号点。求是否可能。

TT 组数据,每组数据由一个空行开始,接着先给定 33 行每行 NN 个整数,分别表示 CC,SS,FF,最后给定 MM 行,每行两个整数表示一条边。

by Error_Eric.

2

5 5
4 3 2 4 3
3 4 3 4 2
2 3 4 4 3
1 2
2 3
3 1
4 1
4 5

4 3
3 2 4 1
2 3 4 4
4 2 3 4
4 2
4 1
4 3

YES
NO

5

2 0
1 2
2 2
2 2

2 1
1 1
2 1
2 1
1 2

2 1
1 1
2 1
1 2
1 2

2 1
1 1
1 2
2 1
1 2

5 4
1 2 3 4 4
2 3 5 4 2
5 3 2 4 2
1 2
1 3
1 4
4 5

YES
YES
NO
YES
NO

提示

For the first test case of the first sample, here is a possible sequence of moves:

Current stall: 1. Keys held: []. Keys in stalls: [3, 4, 3, 4, 2]
(pick up key of color 3)
Current stall: 1. Keys held: [3]. Keys in stalls: [x, 4, 3, 4, 2]
(move from stall 1 to 2, allowed since we have a key of color C_2=3)
Current stall: 2. Keys held: [3]. Keys in stalls: [x, 4, 3, 4, 2]
(pick up key of color 4)
Current stall: 2. Keys held: [3, 4]. Keys in stalls: [x, x, 3, 4, 2]
(move from stall 2 to 1 to 4 to 5, allowed since we have keys of colors C_4=4 and C_5=3)
Current stall: 5. Keys held: [3, 4]. Keys in stalls: [x, x, 3, 4, 2]
(pick up key of color 2 and place key of color 3)
Current stall: 5. Keys held: [2, 4]. Keys in stalls: [x, x, 3, 4, 3]
(move from stall 5 to 4 to 1 to 3, allowed since we have keys of colors C_4=4 and C_3=2)
Current stall: 3. Keys held: [2, 4]. Keys in stalls: [x, x, 3, 4, 3]
(pick up key of color 3 and place key of color 4)
Current stall: 3. Keys held: [2, 3]. Keys in stalls: [x, x, 4, 4, 3]
(move from stall 3 to stall 2 and place key of color 3)
Current stall: 2. Keys held: [2]. Keys in stalls: [x, 3, 4, 4, 3]
(move from stall 2 to stall 1 and place key of color 2)
Current stall: 1. Keys held: []. Keys in stalls: [2, 3, 4, 4, 3]

For the second test case of the first sample, there exists no way for FJ to return a key of color FiF_i to each stall ii and end back at stall 11.

0M1050 \le M \le 10^5, 1Ci,Si,Fi,ui,viN1051 \le C_i, S_i, F_i, u_i, v_i \le N \le 10^5.
1T1001 \le T \le 100, 1N1051 \le \sum N \le 10^5, 1M21051 \le \sum M \le 2\cdot 10^5.

  • Test cases 3-6 satisfy N,M8N,M\le 8.
  • Test cases 7-10 satisfy Ci=FiC_i=F_i.
  • Test cases 11-18 satisfy no additional constraints.