309011: CF1611E2. Escape The Maze (hard version)
Memory Limit:256 MB
Time Limit:2 S
Judge Style:Text Compare
Creator:
Submit:0
Solved:0
Description
Escape The Maze (hard version)
题意翻译
题目简述:现在有一棵树,其中 $1$ 号节点是树根,树上边都是双向边。树上有 $n$ 个点,您有 $k$ 位朋友。每个时刻,您和您的朋友都会顺着树上的边走向另一个节点。 您会获胜当且仅当你走到了叶子节点。 您会输当且仅当您被一个朋友在您获胜之前在任何房间或走廊遇到。 问:至少在树中保留多少个朋友才能使您在游戏中失败?题目描述
The only difference with E1 is the question of the problem. Vlad built a maze out of $ n $ rooms and $ n-1 $ bidirectional corridors. From any room $ u $ any other room $ v $ can be reached through a sequence of corridors. Thus, the room system forms an undirected tree. Vlad invited $ k $ friends to play a game with them. Vlad starts the game in the room $ 1 $ and wins if he reaches a room other than $ 1 $ , into which exactly one corridor leads. Friends are placed in the maze: the friend with number $ i $ is in the room $ x_i $ , and no two friends are in the same room (that is, $ x_i \neq x_j $ for all $ i \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. They don't want to waste too much energy. They ask you to determine if they can win and if they can, what minimum number of friends must remain in the maze so that they can always catch Vlad. In other words, you need to determine the size of the minimum (by the number of elements) subset of friends who can catch Vlad or say that such a subset does not exist.输入输出格式
输入格式
The first line of the input contains an integer $ t $ ( $ 1 \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 $ n $ and $ k $ ( $ 1 \le k < n \le 2\cdot 10^5 $ ) — the number of rooms and friends, respectively. The next line of the test case contains $ k $ integers $ x_1, x_2, \dots, x_k $ ( $ 2 \le x_i \le n $ ) — numbers of rooms with friends. All $ x_i $ are different. The next $ n-1 $ lines contain descriptions of the corridors, two numbers per line $ v_j $ and $ u_j $ ( $ 1 \le u_j, v_j \le n $ ) — numbers of rooms that connect the $ j $ 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 $ n $ over all test cases in the test is not greater than $ 2\cdot10^5 $ .
输出格式
Print $ t $ lines, each line containing the answer to the corresponding test case. The answer to a test case should be $ -1 $ if Vlad wins anyway and a minimal number of friends otherwise.
输入输出样例
输入样例 #1
4
8 2
5 3
4 7
2 5
1 6
3 6
7 2
1 7
6 8
8 4
6 5 7 3
4 7
2 5
1 6
3 6
7 2
1 7
6 8
3 1
2
1 2
2 3
3 2
2 3
3 1
1 2
输出样例 #1
-1
2
1
2