#P9170. [省选联考 2023] 填数游戏

[省选联考 2023] 填数游戏

Problem Description

As everyone knows, Alice and Bob are good friends. Today, they agree to play a game together.

At the beginning, each of them has a blank slip of paper. Next, they will write nn positive integers in the range [1,m][1,m] on their slips, in order. After Alice finishes writing, Bob starts writing his slip after seeing Alice’s slip.

Alice must ensure that the ii-th number she writes belongs to the set SiS_{i}. Bob must ensure that the ii-th number he writes belongs to the set TiT_{i}. It is guaranteed that $1 \leq \left|S_{i}\right|,\left|T_{i}\right| \leq 2$.

Alice likes being the same, so she wants the number of positions where her number equals Bob’s number to be as large as possible. Bob likes being different, so he wants the nn numbers b1,,bnb_{1}, \ldots, b_{n} he writes to be pairwise distinct. On this basis, Bob wants the number of positions where his number equals Alice’s number to be as small as possible.

Let the numbers written by Alice be a1,,ana_{1}, \ldots, a_{n}, and those written by Bob be b1,,bnb_{1}, \ldots, b_{n}. Let XX be the number of indices ii satisfying 1in1 \leq i \leq n and ai=bia_{i}=b_{i}. Then:

  • Alice wants to maximize XX.
  • Bob, under the condition that b1,,bnb_{1}, \ldots, b_{n} are pairwise distinct, wants to minimize XX.

You first want to know whether Bob can ensure that the nn numbers he writes are pairwise distinct. If Bob can do so, you want to know what the value of XX will be when both sides use optimal strategies.

Input Format

This problem has multiple test cases.

The first line contains a positive integer TT, the number of test cases.

Then follow TT test cases, each in the following format:

The first line contains two positive integers n,mn,m, representing the number of integers to be written on the slip and the value range of the integers.

The next nn lines describe the sets SiS_i. In each line, the first integer is Si\left|S_{i}\right|, the number of elements in SiS_{i}, followed by Si\left|S_{i}\right| positive integers describing the elements in SiS_{i}.

The next nn lines describe the sets TiT_i. In each line, the first integer is Ti\left|T_{i}\right|, the number of elements in TiT_{i}, followed by Ti\left|T_{i}\right| positive integers describing the elements in TiT_{i}.

Output Format

For each test case, output one line: if Bob cannot make the nn numbers he writes pairwise distinct, output -1; otherwise, output the value of XX when both sides use optimal strategies.

1
3 4
1 3
2 1 2
2 3 4
2 1 2
2 2 3
2 3 4

1

见附件中的 game/game2.in
见附件中的 game/game2.ans
见附件中的 game/game3.in
见附件中的 game/game3.ans
见附件中的 game/game4.in
见附件中的 game/game4.ans
见附件中的 game/game5.in
见附件中的 game/game5.ans
见附件中的 game/game6.in
见附件中的 game/game6.ans
见附件中的 game/game7.in
见附件中的 game/game7.ans
见附件中的 game/game8.in
见附件中的 game/game8.ans
见附件中的 game/game9.in
见附件中的 game/game9.ans

Hint

[Explanation for Sample 1]

In this sample, S1={3}S_{1}=\{3\}, S2=T1={1,2}S_{2}=T_{1}=\{1,2\}, S3=T3={3,4}S_{3}=T_{3}=\{3,4\}, and T2={2,3}T_{2}=\{2,3\}. Alice has 44 ways to fill in the numbers, listed as follows:

The first: a1=3,a2=1,a3=3a_{1}=3,a_{2}=1,a_{3}=3.

The second: a1=3,a2=1,a3=4a_{1}=3,a_{2}=1,a_{3}=4.

The third: a1=3,a2=2,a3=3a_{1}=3,a_{2}=2,a_{3}=3.

The fourth: a1=3,a2=2,a3=4a_{1}=3,a_{2}=2,a_{3}=4.

Since Bob must ensure that all numbers he fills in are pairwise distinct, he has the following ways to fill:

The first: b1=1,b2=2,b3=3b_{1}=1,b_{2}=2,b_{3}=3.

The second: b1=2,b3=3,b3=4b_{1}=2,b_{3}=3,b_{3}=4.

The third: b1=1,b2=2,b3=4b_{1}=1,b_{2}=2,b_{3}=4.

The fourth: b1=1,b2=3,b3=4b_{1}=1,b_{2}=3,b_{3}=4.

If Alice chooses the first filling method, then to minimize XX, Bob chooses the second method, obtaining X=0X=0.

If Alice chooses the second filling method, then to minimize XX, Bob chooses the first method, obtaining X=0X=0.

If Alice chooses the third filling method, then to minimize XX, Bob chooses the first method, obtaining X=0X=0.

If Alice chooses the fourth filling method, then no matter which method Bob chooses, XX is at least 11.

Therefore, to maximize XX, Alice will choose the fourth filling method.

[Subtasks]

In the table, n\sum n and m\sum m denote the total sum of nn and the total sum of mm over all testdata within the same test point. The meanings of n2\sum n^{2}, m2\sum m^{2}, n3\sum n^{3}, and m3\sum m^{3} are similar. ::cute-table{tuack} | Test Point ID | Constraints | Special Property | |:-:|:-:|:-:| |11 | T20,n,m10T\le20,n,m\le10 | | |22 |^ | ^ | |33 |^ | ^ | |44 |^ | ^ | |55 |^ | ^ | |66 |n,m200,n3,m34107n,m\le200,\sum n^3,\sum m^3\le4\cdot10^7 | A\text A | |77 | ^ | B\text B | |88 | ^ | C\text C | |99 | ^ | D\text D | |1010 | ^ | | |1111 | ^ | ^ | |1212 |n,m2000,n2,m24107n,m\le2000,\sum n^2,\sum m^2\le4\cdot10^7 | ^ | |1313 |^ |^ | |1414 |n,m1.5105,n,m3105n,m\le1.5\cdot10^5,\sum n,\sum m\le3\cdot10^5 |A\text A | |1515 | ^ |B\text B | |1616 | ^ |^ | |1717 | ^ |C\text C | |1818 | ^ |^ | |1919 | ^ | D\text D | |2020 | ^ | ^ | |2121 | ^ | | |2222 | ^ | ^ | |2323 |n,m106,n,m1.5106n,m\le10^6,\sum n,\sum m\le1.5\cdot10^6 | ^| |2424 | ^ | ^ | |2525 | ^ | ^ |

Special Property A: For any 1in1 \leq i \leq n, SiS_i and TiT_i are disjoint, i.e., SiTi=S_i \cap T_i=\emptyset.

Special Property B: n3n \geq 3, and for any 1i<n1 \leq i<n, T1={i,i+1}T_{1} =\{i,i+1\}, and Tn={n,1}T_{n}=\{n,1\}.

Special Property C: For any 1in1 \leq i \leq n, Si=1|S_i|=1.

Special Property D: For any 1in1 \leq i \leq n, Si=TiS_{i}=T_{i}.

[Hint]

Some test points in this problem have large input sizes, so we suggest that you use a more efficient input method.

Translated by ChatGPT 5