#P3876. [TJOI2010] 数字序列

[TJOI2010] 数字序列

Description

Consider a sequence of length nn over digits 0, 1, 2, 3. If it is a valid sequence, it must satisfy both of the following:

  1. No adjacent pair appears as any pattern in {'00', '11', '22', '33', '02', '20', '23', '32', '13', '31'}.

  2. The sequence satisfies mm constraints. Each constraint is of the form {p1, p2, ... pL}, meaning the values at positions p1,p2,,pLp_1, p_2, \ldots, p_L are pairwise distinct. For example, the constraint {1, 5, 11} means the 1st, 5th, and 11th elements are all different.

Given nn, mm, and these mm constraints, determine whether such a valid sequence exists.

Input Format

The first line contains a positive integer TT, the number of test cases. For each test case, the first line contains two integers nn and mm as described above. Then there are mm lines, each describing one constraint: in the ii-th line, the first integer LiL_i is the size of the constraint, followed by LiL_i positive integers giving the positions in the ii-th constraint.

Output Format

Output TT lines. For each test case, print Yes if a valid sequence exists; otherwise, print No.

2
7 2
3 1 2 4
4 2 4 5 7
3 1
2 1 1

Yes
No

Hint

Constraints:

T10T \le 10, 1n1000001 \le n \le 100000, 0m50000 \le m \le 5000, 1Li1001 \le L_i \le 100, 1pin1 \le p_i \le n.

Time limit per test point: 1 second.

In the first sample, the sequence 0103012 is a valid sequence.

Translated by ChatGPT 5