#USACOC2212C. Bracelet Crossings
Bracelet Crossings
Bessie the cow enjoys arts and crafts. In her free time, she has made bracelets, conveniently numbered . The thbracelet is painted color out of a set of different colors. Afterconstructing the bracelets, Bessie placed them on a table for display (which wecan think of as the 2D plane). She was careful to arrange the bracelets tosatisfy the following three conditions:
- Every bracelet was a single closed polygonal chain -- a series of vertices(points) connected sequentially by line segments, where the first and lastpoints are the same (Feel welcome to consult the wikipedia page formore detail: polygonal chain),
- No bracelet intersected itself (this corresponds to a "simple" polygonalchain); and
- No two bracelets intersected.
Unfortunately, right after Bessie arranged the bracelets in such a carefulmanner, Farmer John drove by in his tractor, shaking the table and causing thebracelets to shift around and possibly break into multiple (not necessarilyclosed or simple) polygonal chains! Afterward, Bessie wanted to check whetherthe three conditions above still held. However, it was dark, so she couldn't seethe bracelets anymore.
Fortunately, Bessie had a flashlight. She chose verticallines and for each line, she swept the beam of theflashlight along that line from to , recording the colorsof all bracelets she saw in the order they appeared. Luckily, no beam crossedover any vertex of any polygonal chain or two line segments at the same time.Furthermore, for each beam, every color that appeared appeared exactly twice.
Can you help Bessie use this information to determine whether it is possiblethat the bracelets still satisfy all three of the conditions above?
Input Format
Each input case contains sub-cases that must all solvedindependently to solve the full input case. Consecutive test cases are separated by newlines.
The first line of the input contains . Each of the sub-test cases thenfollow.
The first line of each sub-test case contains two integers and . Eachsub-test case then contains additional lines. For each from to ,the -th additional line contains an integer (, even), followed by integers (, every appears zero or two times). This means thatwhen Bessie swept her flashlight from to , sheencountered the colors in that order.
Output Format
For each sub-test case, print YES if it is possible for the three conditionsabove to be satisfied. Otherwise, print NO.
5
1 2
2 1 1
2 1 1
1 3
2 1 1
0
2 1 1
2 1
4 1 2 1 2
4 2
6 1 2 2 3 3 1
6 1 2 4 4 2 1
2 2
4 1 1 2 2
4 2 2 1 1
5
1 2
2 1 1
2 1 1
1 3
2 1 1
0
2 1 1
2 1
4 1 2 1 2
4 2
6 1 2 2 3 3 1
6 1 2 4 4 2 1
2 2
4 1 1 2 2
4 2 2 1 1
Scoring
- Test case 2 satisfies .
- Test cases 3-5 satisfy .
- Test cases 6-8 satisfy .
- Test cases 9-14 satisfy .
- Test cases 15-20 satisfy no additional constraints.
Problem Credit
Problem credits: Richard Qi