#P6632. [ZJOI2020] 染色游戏

[ZJOI2020] 染色游戏

Description

Alice and Bob are playing a coloring game. The game is played on a connected graph with NN vertices and MM edges, where N1MNN - 1 \le M \le N. Bob wants to trap Alice, while Alice wants to escape Bob’s encirclement.

At the start of the game, Alice colors vertex 11 black, meaning she occupies vertex 11. Bob colors all vertices in the set SS white, meaning he occupies these S|S| vertices. It is guaranteed that 1S1 \notin S. 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 TT in the graph. If, when the game ends, all vertices in TT are colored white, then Bob has successfully trapped Alice and Bob wins. Otherwise, at least one vertex in TT is colored black, and Alice wins. Note that TT may contain vertices from SS and may also contain vertex 11.

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 kk turns, what is the minimum such kk? Alice will only skip her first kk turns, and in the remaining turns she will play optimally. You may understand this as Bob taking kk 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 00. If Bob still cannot win when k=1000000k = 1000000, output 10000001000000.

Since the graph may be very large, it is generated in the following way.

  • First, generate an empty graph with nn vertices labeled from 11 to nn.
  • Then add mm chains. The ii-th chain is denoted as (ui,vi,li)(u_i, v_i, l_i), where 1ui,vin1 \le u_i, v_i \le n and uiviu_i \neq v_i.
    • First, add lil_i vertices, denoted as x1i,x2i,...,xliix_1^i, x_2^i, ..., x_{l_i}^i.
    • 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 lil_i vertices added in this chain will not be connected to any other vertices, i.e. x1i...xliix_1^i ... x_{l_i}^i from different chains are all distinct vertices. In particular, if li=0l_i = 0, then no new vertices are added, and an undirected edge is added directly between (ui,vi)(u_i, v_i).

It is guaranteed that all vertices in the sets SS and TT are among the initial nn vertices.

Input Format

The first line contains an integer CC, the number of test cases.
For each test case:

  • The first line contains four integers n,m,S,Tn, m, |S|, |T|, $(1 \le |S| \le n - 1, 1 \le |T| \le n, n - 1 \le m \le n)$.
  • The next mm lines each contain three non-negative integers ui,vi,liu_i, v_i, l_i, (1ui,vin,0li106)(1 \le u_i, v_i \le n, 0 \le l_i \le 10^6), describing the ii-th chain in the statement.
  • The next line contains S|S| integers s1...sSs_1 ... s_{|S|}, the elements of the set SS (2sin2 \le s_i \le n, all distinct).
  • The next line contains T|T| integers t1...tTt_1 ... t_{|T|}, the elements of the set TT (1tin1 \le t_i \le n, 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 uiviu_i \neq v_i (no self-loops), that there are no identical pairs (ui,vi)(u_i, v_i) (no multiple edges), and that the resulting graph is connected.

Output Format

Output CC lines. For each test case, output the minimum number of turns kk that Alice must skip so that Bob can win. If Bob already wins on the original graph, output 00. If Bob still cannot win when k=1000000k = 1000000, output 10000001000000.

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 100%100\% of the testdata, m=nm = n or n1n - 1, 3n5003 \le n \le 500, C=10000C = 10000, 0li1060 \le l_i \le 10^6, 1Sn11 \le |S| \le n - 1, 1Tn1 \le |T| \le n, and it is guaranteed that the graph contains no cycle (counting only the first nn vertices) with more than 100100 vertices. In each test point, at most 1010 test cases satisfy n>50n > 50, and at most 10001000 test cases satisfy n>20n > 20.

Translated by ChatGPT 5