CF BUDDY
← Problems·

1971H · ±1

2100 · 2-sat, dfs and similar, graphs

Problem: Bob has a grid with 33 rows and nn columns, each of which contains either aia_i or ai-a_i for some integer 1in1 \leq i \leq n. For example, one possible grid for n=4n=4 is shown below:

[a1a2a3a2a4a4a1a3a1a2a2a4]\begin{bmatrix} a_1 & -a_2 & -a_3 & -a_2 \\ -a_4 & a_4 & -a_1 & -a_3 \\ a_1 & a_2 & -a_2 & a_4 \end{bmatrix}

Alice and Bob play a game as follows:

  • Bob shows Alice his grid.
  • Alice gives Bob an array a1,a2,,ana_1, a_2, \dots, a_n of her choosing, whose elements are all 1\mathbf{-1} or 1\mathbf{1}.
  • Bob substitutes these values into his grid to make a grid of 1-1s and 11s.
  • Bob sorts the elements of each column in non-decreasing order.
  • Alice wins if all the elements in the middle row are 11; otherwise, Bob wins.

For example, suppose Alice gives Bob the array [1,1,1,1][1, -1, -1, 1] for the grid above. Then the following will happen (colors are added for clarity):

[a1a2a3a2a4a4a1a3a1a2a2a4][1,1,1,1][111111111111]sort each column[111111111111].\begin{bmatrix} \color{red}{a_1} & \color{green}{-a_2} & \color{blue}{-a_3} & \color{green}{-a_2} \\ -a_4 & a_4 & \color{red}{-a_1} & \color{blue}{-a_3} \\ \color{red}{a_1} & \color{green}{a_2} & \color{green}{-a_2} & a_4 \end{bmatrix} \xrightarrow{[\color{red}{1},\color{green}{-1},\color{blue}{-1},1]} \begin{bmatrix} \color{red}{1} & \color{green}{1} & \color{blue}{1} & \color{green}{1} \\ -1 & 1 & \color{red}{-1} & \color{blue}{1} \\ \color{red}{1} & \color{green}{-1} & \color{green}{1} & 1 \end{bmatrix} \xrightarrow{\text{sort each column}} \begin{bmatrix} -1 & -1 & -1 & 1 \\ \mathbf{1} & \mathbf{1} & \mathbf{1} & \mathbf{1} \\ 1 & 1 & 1 & 1 \\ \end{bmatrix}\,. Since the middle row is all 11, Alice wins.

Given Bob's grid, determine whether or not Alice can choose the array aa to win the game.

Input Format: The first line contains a single integer tt (1t10001 \leq t \leq 1000) — the number of test cases.

The first line of each test case contains a single integer nn (2n5002 \leq n \leq 500) — the number of columns of Bob's grid.

The next three lines each contain nn integers, the ii-th of which contains gi,1,gi,2,,gi,ng_{i,1}, g_{i,2}, \dots, g_{i,n} (ngi,jn-n \leq g_{i,j} \leq n, gi,j0g_{i,j} \neq 0), representing Bob's grid.

If cell x>0x > 0 is in the input, that cell in Bob's grid should contain axa_x; if x<0x < 0 is in the input, that cell in Bob's grid should contain ax-a_{-x}. See the sample input and notes for a better understanding.

Output Format: For each test case, output "YES" (without quotes) if Alice can win, and "NO" (without quotes) otherwise.

You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", and "Yes" will be recognized as a positive response).

Note: The first test case is described in the statement.

In the second test case, Bob's grid is as follows:

[a1a2a1a2a2a2]\begin{bmatrix} a_1 & a_2 \\ -a_1 & -a_2 \\ a_2 & -a_2 \end{bmatrix}

For the last column to have 11 in the middle row when sorted, Alice must pick a2=1a_2 = -1. However, it is then impossible to choose a1a_1 such that the first column has 11 in the middle when sorted. Thus, Alice cannot win.

In the third test case, Alice can pick a=[1,1,1,1,1]a = [1,1,1,1,1].

Sample Cases

Case 1

Input

4
4
1 -2 -3 -2
-4 4 -1 -3
1 2 -2 4
2
1 2
-1 -2
2 -2
5
1 2 3 4 5
-2 3 -4 -5 -1
3 -5 1 2 2
6
1 3 -6 2 5 2
1 3 -2 -3 -6 -5
-2 -1 -3 2 3 1

Output

YES
NO
YES
NO

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback