CF1790G.Tokens on Graph

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given an undirected connected graph, some vertices of which contain tokens and/or bonuses. Consider a game involving one player — you.

You can move tokens according to the following rules:

  • At the beginning of the game, you can make exactly one turn: move any token to any adjacent vertex.
  • If the movement of the token ended on the bonus, then you are allowed to make another turn with any other token.

You can use different bonuses in any order. The same bonus can be used an unlimited number of times. Bonuses do not move during the game.

There can be several tokens in one vertex at the same time, but initially there is no more than one token in each vertex.

The vertex with number 11 is the finish vertex, and your task is to determine whether it is possible to hit it with any token by making turns with the tiles according to the rules described above. If a token is initially located at the vertex of 11, then the game is considered already won.

The finish line is in black, the bonuses are in red, the chips are in grey.

For example, for a given graph, you can reach the finish line with a chip from the 88th vertex by making the following sequence of turns:

  1. Move from the 88-th vertex to the 66-th.
  2. Move from the 77-th vertex to the 55-th.
  3. Move from the 66-th vertex to the 44-th.
  4. Move from the 55-th vertex to the 66-th.
  5. Move from the 44-th vertex to the 22-nd.
  6. Move from the 66-th vertex to the 44-th.
  7. Move from the 22-nd vertex to the 11-st vertex, which is the finish.

给你一个无向连通图,其中某些顶点上放置有棋子和/或奖励点。考虑一个仅由一名玩家(即你)参与的游戏。

你可以按照以下规则移动棋子:

  • 游戏开始时,你恰好可以进行一次操作:将任意一个棋子移动到其任意一个相邻顶点。
  • 若该棋子的移动终点是一个奖励点,则你被允许再进行一次操作——即用任意其他棋子再执行一次移动。

你可以按任意顺序使用不同的奖励点;同一个奖励点可被重复使用任意多次。奖励点在游戏过程中位置固定,不会移动。

同一顶点上可同时存在多个棋子,但初始状态下每个顶点至多只有一个棋子。

编号为 11 的顶点是终点,你的任务是判断:是否可以通过按上述规则进行操作,使得某个棋子最终到达顶点 11?若某个棋子初始时就位于顶点 11,则游戏视为已经获胜。

图中终点为黑色,奖励点为红色,棋子为灰色。

例如,在给定的图中,你可以通过以下操作序列,使位于第 88 个顶点的棋子抵达终点:

  1. 将棋子从第 88 个顶点移至第 66 个顶点;
  2. 将棋子从第 77 个顶点移至第 55 个顶点;
  3. 将棋子从第 66 个顶点移至第 44 个顶点;
  4. 将棋子从第 55 个顶点移至第 66 个顶点;
  5. 将棋子从第 44 个顶点移至第 22 个顶点;
  6. 将棋子从第 66 个顶点移至第 44 个顶点;
  7. 将棋子从第 22 个顶点移至第 11 个顶点(即终点)。

输入格式

The first line of input data contains a single integer tt (1≤t≤1041 \le t \le 10^4) — number of test cases in the test. The descriptions of the test cases follow.

The first line of the description of each test case contains two integers nn and mm (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5, 0≤m≤2⋅1050 \le m \le 2 \cdot 10^5) — the number of vertices and edges in the graph, respectively.

The second line of the description of each test case contains two integers pp and bb (1≤p≤n,0≤b≤n1 \le p \le n, 0 \le b \le n) — the number of tokens and bonuses, respectively.

The third line of the description of each test case contains pp different integers from 11 to nn — the indices of the vertices in which the tokens are located.

The fourth line of the description of each input data set contains bb different integers from 11 to nn — the indices of the vertices in which the bonuses are located. Note that the value of bb can be equal to 00. In this case, this line is empty.

There can be both a token and a bonus in one vertex at the same time.

The next mm lines of the description of each test case contain two integers uiu_i and viv_i (1≤ui,vi≤n1 \le u_i, v_i \le n, ui≠viu_i \ne v_i) — vertices connected by the ii-th edge. There is at most one edge between each pair of vertices. The given graph is connected, that is, from any vertex you can get to any one by moving along the edges.

The test cases are separated by an empty string.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5. Similarly, it is guaranteed that the sum of mm over all input data sets does not exceed 2⋅1052 \cdot 10^5.

输入数据的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的描述第一行包含两个整数 nn 和 mm(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5,0≤m≤2⋅1050 \le m \le 2 \cdot 10^5),分别表示图中的顶点数和边数。

每个测试用例的描述第二行包含两个整数 pp 和 bb(1≤p≤n1 \le p \le n,0≤b≤n0 \le b \le n),分别表示令牌数量和奖励数量。

每个测试用例的描述第三行包含 pp 个互不相同的整数,取值范围为 11 到 nn,表示令牌所在顶点的编号。

每个测试用例的描述第四行包含 bb 个互不相同的整数,取值范围为 11 到 nn,表示奖励所在顶点的编号。注意,bb 的值可能为 00;此时该行为空。

同一顶点上可以同时存在一个令牌和一个奖励。

每个测试用例描述的接下来 mm 行,每行包含两个整数 uiu_i 和 viv_i(1≤ui,vi≤n1 \le u_i, v_i \le n,ui≠viu_i \ne v_i),表示第 ii 条边所连接的两个顶点。任意一对顶点之间至多存在一条边。给定图是连通的,即从任意顶点出发均可沿边到达其余任意顶点。

各测试用例之间以空行分隔。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5;同样,保证所有测试用例的 mm 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, print YES in a separate line if you can reach the finish with some token, and NO otherwise.

You can output YES and NO in any case (for example, the strings yEs, yes, Yes and YES will be recognized as a positive response).

对于每个测试用例,如果你能使用某个令牌到达终点,则在单独一行中输出 YES;否则输出 NO。

你可以以任意大小写形式输出 YES 和 NO(例如,字符串 yEs、yes、Yes 和 YES 均被视为肯定回答)。

输入输出样例

  • 输入#1

    6
    8 10
    2 4
    7 8
    2 4 5 6
    1 2
    2 3
    2 4
    3 4
    3 5
    4 6
    5 6
    5 7
    6 8
    7 8
    
    5 4
    1 1
    5
    3
    1 2
    2 3
    3 4
    4 5
    
    2 1
    1 0
    2
    
    1 2
    
    4 3
    1 2
    2
    3 4
    1 2
    2 3
    2 4
    
    5 4
    3 2
    5 3 4
    2 4
    1 2
    2 3
    3 4
    4 5
    
    1 0
    1 1
    1
    1

    输出#1

    YES
    NO
    YES
    YES
    YES
    YES

说明/提示

  • The first test case is explained in the statement.

  • In the second test case, there is only one token which can make only one turn, and it cannot reach the finish.

  • In the third test case, the token can reach the finish line in 11 turn.

  • In the fourth test case, you need to make just one turn from 22 to 11.

  • In the sixth test case, the token is initially at node number 11, so we win immediately.

  • 第一个测试用例已在题目描述中说明。

  • 在第二个测试用例中,仅有一个棋子,它只能进行一次转向,且无法到达终点。

  • 在第三个测试用例中,棋子可在 11 轮内到达终点线。

  • 在第四个测试用例中,你只需从节点 22 向节点 11 转向一次即可。

  • 在第六个测试用例中,棋子初始位于节点 11,因此我们立即获胜。

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

首页