#P7989. Bracelet Validity Check

    ID: 21173 Type: Default 1000ms 256MiB

Bracelet Validity Check

Bracelet Validity Check

Bessie the cow made N bracelets, each colored uniquely from 1 to N, and originally arranged them on a table such that each bracelet is a simple closed polygonal chain (i.e. it does not self‐intersect) and no two bracelets intersect. Unfortunately, after an accident, the bracelets might have broken into pieces. In the dark, Bessie uses a flashlight to scan along M vertical lines at x = 1, 2, ..., M. For each vertical line, she records the colors of the bracelet segments in the order they appear as the light rays travel from y = -∞ to y = ∞. It is guaranteed that:

  • No light beam goes through any vertex or touches two segments simultaneously.
  • Along each vertical line, every color appears exactly twice.

Using the recorded orders, determine whether the bracelets still satisfy all three conditions (i.e. every bracelet forms a closed, simple polygonal chain and no two bracelets intersect).

Observation: When a non‐self-intersecting closed curve is intersected by a vertical line, the two intersection points occur in a non crossing (i.e. non interleaving) order relative to any other disjoint closed curve. In other words, if a color i appears at positions a and b (with a < b) and another color j appears at positions c and d (with c < d), then it must not be that a < c < b < d (or symmetrically c < a < d < b) because that would force the curves to cross. Hence, for each vertical line, if any two bracelets have their appearances interleaved (i.e. one of them starts between the two appearances of the other and ends after), then the configuration is invalid.

Your task is to process the records from the M vertical lines and output YES if the bracelets still satisfy the conditions, or NO otherwise.

Note: All formulas are formatted in LaTeX. For example, the number of bracelets is given as $1\le N\le 50$, and the vertical lines are at $x = 1, 2, \ldots, M$.

inputFormat

The first line contains two integers N and M ($1 \le N \le 50$, $1 \le M \le 50$), representing the number of bracelets and the number of vertical lines scanned respectively.

Then follow M lines. Each of these lines contains exactly 2N integers. Each line represents the order of bracelet colors encountered along that vertical line (from y = -∞ to y = ∞). Each color from 1 to N appears exactly twice in each line.

outputFormat

Output a single line containing YES if the bracelets satisfy all the required conditions, or NO if not.

sample

2 1
1 1 2 2
YES

</p>