CF1611E1.Escape The Maze (easy version)

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

The only difference with E2 is the question of the problem..

Vlad built a maze out of nn rooms and n−1n-1 bidirectional corridors. From any room uu any other room vv can be reached through a sequence of corridors. Thus, the room system forms an undirected tree.

Vlad invited kk friends to play a game with them.

Vlad starts the game in the room 11 and wins if he reaches a room other than 11, into which exactly one corridor leads.

Friends are placed in the maze: the friend with number ii is in the room xix_i, and no two friends are in the same room (that is, xi≠xjx_i \neq x_j for all i≠ji \neq j). Friends win if one of them meets Vlad in any room or corridor before he wins.

For one unit of time, each participant of the game can go through one corridor. All participants move at the same time. Participants may not move. Each room can fit all participants at the same time.

Friends know the plan of a maze and intend to win. Vlad is a bit afraid of their ardor. Determine if he can guarantee victory (i.e. can he win in any way friends play).

In other words, determine if there is such a sequence of Vlad's moves that lets Vlad win in any way friends play.

与 E2 唯一的区别在于问题本身。

弗拉德用 nn 个房间和 n−1n-1 条双向走廊建造了一个迷宫。从任意房间 uu 出发,均可通过一系列走廊到达任意其他房间 vv。因此,整个房间系统构成一棵无向树。

弗拉德邀请了 kk 位朋友一起玩游戏。

游戏开始时,弗拉德位于房间 11;若他抵达一个非房间 11、且仅与一条走廊相连的房间,则获胜。

朋友们被安置在迷宫中:编号为 ii 的朋友位于房间 xix_i,且任意两位朋友均不在同一房间(即对所有 i≠ji \neq j,均有 xi≠xjx_i \neq x_j)。若在弗拉德获胜前,有任意一位朋友在某个房间或某条走廊上与弗拉德相遇,则朋友们获胜。

每单位时间内,每位参与者均可穿过一条走廊。所有参与者同时移动。参与者也可选择不动。每个房间可同时容纳所有参与者。

朋友们知晓迷宫的全部结构,并决心获胜。弗拉德对他们的热情略感畏惧。请判断弗拉德能否保证获胜(即:无论朋友们如何行动,他是否总能获胜)。

换言之,请判断是否存在一个弗拉德的移动序列,使得无论朋友们如何行动,弗拉德均能获胜。

输入格式

The first line of the input contains an integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases in the input. The input contains an empty string before each test case.

The first line of the test case contains two numbers nn and kk (1≤k<n≤2⋅1051 \le k \lt n \le 2\cdot 10^5) — the number of rooms and friends, respectively.

The next line of the test case contains kk integers x1,x2,…,xkx_1, x_2, \dots, x_k (2≤xi≤n2 \le x_i \le n) — numbers of rooms with friends. All xix_i are different.

The next n−1n-1 lines contain descriptions of the corridors, two numbers per line vjv_j and uju_j (1≤uj,vj≤n1 \le u_j, v_j \le n) — numbers of rooms that connect the jj corridor. All corridors are bidirectional. From any room, you can go to any other by moving along the corridors.

It is guaranteed that the sum of the values nn over all test cases in the test is not greater than 2⋅1052\cdot10^5.

输入的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 表示输入中测试用例的数量。每个测试用例前均有一行空行。

每个测试用例的第一行包含两个整数 nn 和 kk(1≤k<n≤2⋅1051 \le k \lt n \le 2\cdot 10^5)—— 分别表示房间数量和朋友数量。

测试用例的第二行包含 kk 个整数 x1,x2,…,xkx_1, x_2, \dots, x_k(2≤xi≤n2 \le x_i \le n)—— 表示有朋友的房间编号。所有 xix_i 互不相同。

接下来的 n−1n-1 行描述走廊,每行包含两个整数 vjv_j 和 uju_j(1≤uj,vj≤n1 \le u_j, v_j \le n)—— 表示第 jj 条走廊所连接的两个房间编号。所有走廊均为双向通行。从任意一个房间出发,均可通过走廊到达其他任意房间。

保证所有测试用例中 nn 的总和不超过 2⋅1052\cdot10^5。

输出格式

Print tt lines, each line containing the answer to the corresponding test case. The answer to a test case should be "YES" if Vlad can guarantee himself a victory and "NO" otherwise.

You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" will all be recognized as positive answers).

输出 tt 行,每行包含对应测试用例的答案。若弗拉德能确保自己获胜,则该测试用例的答案为 "YES";否则为 "NO"。

你可以以任意大小写形式输出每个字母(例如,字符串 "yEs"、"yes"、"Yes" 和 "YES" 均会被识别为肯定回答)。

输入输出样例

  • 输入#1

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

    输出#1

    YES
    NO
    YES
    NO

说明/提示

In the first test case, regardless of the strategy of his friends, Vlad can win by going to room 44. The game may look like this:

The original locations of Vlad and friends. Vlad is marked in green, friends — in red.

Locations after one unit of time.

End of the game.

Note that if Vlad tries to reach the exit at the room 88, then a friend from the 33 room will be able to catch him.

在第一个测试用例中,无论朋友们采取何种策略,弗拉德都可以通过前往房间 44 获胜。游戏过程可能如下所示:

弗拉德与朋友们的初始位置。弗拉德用绿色标记,朋友们用红色标记。

经过一个时间单位后的位置。

游戏结束。

注意:若弗拉德试图抵达房间 88 处的出口,则来自房间 33 的朋友将能够抓住他。

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

首页