#P12414. 「YLLOI-R1-T3」一路向北

    ID: 11724 远端评测题 500ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>洛谷原创O2优化洛谷月赛Ad-hoc

「YLLOI-R1-T3」一路向北

Description

Given nn queues, each containing mm positive integers, all of which are less than or equal to nn, the ii-th queue's jj-th element is ai,ja_{i,j}, where ai,1a_{i,1} is the head of the queue and ai,ma_{i,m} is the tail of the queue.

Now, you hold the number 00. You need to select one queue, place 00 at its tail, and take the head of the queue into your hand.

Next, you will repeat the operation until you take 00 back into your hand:

  • Let the number in your hand be pp. Place it at the tail of the pp-th queue and take the head of the pp-th queue into your hand.

Now, Little Y wants to know if, over an infinite amount of time, you can avoid taking back 00. If you can, output Yes, otherwise, output No.

Input Format

There are multiple test cases for this problem.

The first line contains an integer TT, representing the number of test cases.

For each test case:

  • The first line contains two integers nn and mm.
  • The next nn lines, each containing mm integers, represent the ii-th queue's elements ai,ja_{i,j}.

Output Format

For each test case, output one line containing Yes if you can avoid taking back 00, otherwise output No.

1
3 2
2 2
3 3
1 1
No
1
3 2
2 1
3 3
2 2
Yes

Hint

Explanation

Sample 1:

Below is a simulation where 00 is initially placed into the first queue.

// Number in hand:
0
// Queue contents (leftmost is the head, rightmost is the tail):
2 2
3 3
1 1
// Number in hand:
2
// Queue contents:
2 0
3 3
1 1
// Number in hand:
3
// Queue contents:
2 0
3 2
1 1
// Number in hand:
1
// Queue contents:
2 0
3 2
1 3
// Number in hand:
2
// Queue contents:
0 1
3 2
1 3
// Number in hand:
3
// Queue contents:
0 1
2 2
1 3
// Number in hand:
1
// Queue contents:
0 1
2 2
3 3
// Number in hand:
0
// Queue contents:
1 1
2 2
3 3

Sample 2:

By simulation, we can see that only when 00 is initially placed into the first queue can it never be picked up again.
This is because after several rounds, the second queue will be filled with the number 22, and the number in hand will also be 22, so the process will loop indefinitely within the second queue.

Constraints

This problem uses subtask scoring.

  • Subtask 1 (20 points): n2n \leq 2.
  • Subtask 2 (10 points): ai,j=i\forall a_{i,j} = i.
  • Subtask 3 (20 points): n×m1000n \times m \leq 1000.
  • Subtask 4 (50 points): No special restrictions.

For all data:

  • 1T101 \leq T \leq 10.
  • 1n×m1051 \leq n \times m \leq 10^5.
  • 1ai,jn1 \leq a_{i,j} \leq n.