#P12414. 「YLLOI-R1-T3」一路向北
「YLLOI-R1-T3」一路向北
Description
Given queues, each containing positive integers, all of which are less than or equal to , the -th queue's -th element is , where is the head of the queue and is the tail of the queue.
Now, you hold the number . You need to select one queue, place at its tail, and take the head of the queue into your hand.
Next, you will repeat the operation until you take back into your hand:
- Let the number in your hand be . Place it at the tail of the -th queue and take the head of the -th queue into your hand.
Now, Little Y wants to know if, over an infinite amount of time, you can avoid taking back . If you can, output Yes, otherwise, output No.
Input Format
There are multiple test cases for this problem.
The first line contains an integer , representing the number of test cases.
For each test case:
- The first line contains two integers and .
- The next lines, each containing integers, represent the -th queue's elements .
Output Format
For each test case, output one line containing Yes if you can avoid taking back , 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 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 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 , and the number in hand will also be , so the process will loop indefinitely within the second queue.
Constraints
This problem uses subtask scoring.
- Subtask 1 (20 points): .
- Subtask 2 (10 points): .
- Subtask 3 (20 points): .
- Subtask 4 (50 points): No special restrictions.
For all data:
- .
- .
- .
京公网安备 11011102002149号