#P12414. Infinite Queue Process Avoiding Zero

    ID: 14517 Type: Default 1000ms 256MiB

Infinite Queue Process Avoiding Zero

Infinite Queue Process Avoiding Zero

We are given (n) queues, each of which contains (m) positive integers with values at most (n). For (1 \le i \le n) and (1 \le j \le m) the (j)th element of the (i)th queue is denoted by (a_{i,j}) (where (a_{i,1}) is the front and (a_{i,m}) is the rear).

Initially you hold the number (0). You may choose one queue and perform the following operation: insert the number in your hand at the rear of that chosen queue and then remove (and take) the front element of that queue to become the new number in your hand.

After this initial move, you then repeat the following process indefinitely until you pick up (0) again:

  • Let \(p\) be the number currently in your hand. Insert \(p\) at the rear of the \(p\)th queue and then remove (and take) its front element to become the new number in your hand.

Determine whether there exists a choice of the initial queue such that, with infinite operations, you can avoid picking up \(0\) ever again. If such a choice exists, print Yes, otherwise print No.

Note: All formulas are written in \(\LaTeX\).

inputFormat

The first line contains two integers (n) and (m) (the number of queues and the number of elements in each queue).

Then (n) lines follow. The (i)th line contains (m) positive integers (a_{i,1}, a_{i,2}, \ldots, a_{i,m}) (each (\le n)), representing the (i)th queue in order from front to rear.

outputFormat

Output a single line containing Yes if there exists an initial queue selection such that you can avoid ever picking up (0) (i.e. the process will never return (0) in an infinite number of operations), or No otherwise.

sample

2 2
1 2
2 1
No