#P6632. [ZJOI2020] 染色游戏
[ZJOI2020] 染色游戏
Description
Alice and Bob are playing a coloring game. The game is played on a connected graph with vertices and edges, where . Bob wants to trap Alice, while Alice wants to escape Bob’s encirclement.
At the start of the game, Alice colors vertex black, meaning she occupies vertex . Bob colors all vertices in the set white, meaning he occupies these vertices. It is guaranteed that . Then the two players take turns to act, with Alice moving first. On each turn, the current player may start from a vertex occupied by themselves (black for Alice, white for Bob), choose an adjacent uncolored vertex, occupy it, and color it with their own color. If there is no vertex that can be colored, the player must skip this turn. When all vertices have been colored, the game ends.
Alice and Bob have agreed on a non-empty vertex set in the graph. If, when the game ends, all vertices in are colored white, then Bob has successfully trapped Alice and Bob wins. Otherwise, at least one vertex in is colored black, and Alice wins. Note that may contain vertices from and may also contain vertex .
Both Alice and Bob will use optimal strategies. Bob notices that in some positions, Alice has a big advantage. If Alice could voluntarily skip some of her turns at the beginning to reach a fairer position, the game would be more interesting. Bob wants to know: if Bob can win after Alice skips her first turns, what is the minimum such ? Alice will only skip her first turns, and in the remaining turns she will play optimally. You may understand this as Bob taking extra turns before Alice’s first move. Note that if Bob has no legal move on a turn that Alice skips, then Bob still must skip his turn according to the rules. If Bob already wins on the original graph, output . If Bob still cannot win when , output .
Since the graph may be very large, it is generated in the following way.
- First, generate an empty graph with vertices labeled from to .
- Then add chains. The -th chain is denoted as , where and .
- First, add vertices, denoted as .
- Then add undirected edges between $(u_i, x_1^i), (x_1^i, x_2^i), (x_2^i, x_3^i), ... , (x_{l_i-1}^i, x_{l_i}^i), (x_{l_i}^i, v_i)$.
- After this operation, the vertices added in this chain will not be connected to any other vertices, i.e. from different chains are all distinct vertices. In particular, if , then no new vertices are added, and an undirected edge is added directly between .
It is guaranteed that all vertices in the sets and are among the initial vertices.
Input Format
The first line contains an integer , the number of test cases.
For each test case:
- The first line contains four integers , $(1 \le |S| \le n - 1, 1 \le |T| \le n, n - 1 \le m \le n)$.
- The next lines each contain three non-negative integers , , describing the -th chain in the statement.
- The next line contains integers , the elements of the set (, all distinct).
- The next line contains integers , the elements of the set (, all distinct).
That is, each test case is given in the following format:
n m |S||T|
u_1 v_1 l_1
u_2 v_2 l_2
···
u_m v_m l_m
s_1 s_2 ··· s_|S|
t_1 t_2 ··· t_|T|
It is guaranteed that (no self-loops), that there are no identical pairs (no multiple edges), and that the resulting graph is connected.
Output Format
Output lines. For each test case, output the minimum number of turns that Alice must skip so that Bob can win. If Bob already wins on the original graph, output . If Bob still cannot win when , output .
5
6 5 2 2
1 2 0
2 3 0
2 4 0
3 5 0
4 6 0
5 6
3 4
6 5 2 2
1 2 1
2 3 0
2 4 0
3 5 0
4 6 0
5 6
3 4
5 4 2 2
1 2 1
1 3 1
2 4 0
3 5 0
4 5
2 3
8 8 1 2
1 2 2
2 3 1
3 4 0
4 5 0
5 6 0
6 7 0
7 2 1
5 8 0
8
3 7
8 8 1 2
1 2 3
2 3 0
3 4 0
4 5 0
5 6 0
6 7 0
7 2 0
5 8 0
8
3 7
1
0
0
0
1
Hint

For of the testdata, or , , , , , , and it is guaranteed that the graph contains no cycle (counting only the first vertices) with more than vertices. In each test point, at most test cases satisfy , and at most test cases satisfy .
Translated by ChatGPT 5
京公网安备 11011102002149号