Amidakuji
InterviewTime limit8sMemory limit1024 MB
Given an amidakuji and a start line p, find the position of one added bar that routes p's trace to line q, or report OK or NG.
- Level
Medium4 of 10
- Topics
- Brute force, Simulation, Array
- Solved
- No attempts yet
Problem
Amidakuji is a lottery method popular in East Asia. It is often used to hand out a number of gifts to people or to assign a number of tasks.
Figure B-1 shows an example of amidakuji. A number of vertical lines are drawn, and the number of lines equals the number of participants. Some horizontal bars are drawn between pairs of adjacent vertical lines. The number and positions of the bars are arbitrary, but no two bars may share an endpoint. Usually, many horizontal bars are drawn at random.
To use an amidakuji, first write the names of the items, gifts or tasks, at the bottom ends of the vertical lines. Then each participant chooses a top end of a vertical line, all different from one another. Participants trace down the chosen line from the top. When they reach a horizontal bar, they follow it to the adjacent vertical line it connects to, and continue downward. When the bottom end of a vertical line is reached, the item written there is the gift or task awarded.
Figure B-2 shows the case where the top end P is chosen. In this case, the item at R is obtained.
Assume that the participant choosing P in Figure B-2 wants the item at Q, which is not reached. A special rule just introduced may grant that wish. The new rule allows adding a single horizontal bar at an arbitrary position. Adding the bar marked X in Figure B-3 makes the trace reach Q.
Your task is to write a program that finds the position of a horizontal bar to add to a given amidakuji, so that the trace from the top end of the specified vertical line reaches the bottom end of the specified vertical line.
Input
The input consists of multiple datasets, each in the following format.
All input items are positive integers. is the number of vertical lines, and is the number of horizontal bars in the original amidakuji. The participant choosing the -th line from the left wants the trace to reach the bottom end of the -th line from the left. It holds that , , , and .
are the positions of the horizontal bars, where is the position of the -th bar from the top. All bar positions are distinct. It holds that , and the bar connects the -th line and the -th line.
The end of the input is a line with four zeros. The number of datasets does not exceed 300.
Output
For each dataset, output one of the following on a single line.
- If can be reached from without adding a bar, output
OK. - If not, and adding a single bar cannot make reachable from , output
NG. - Otherwise, output two integers and separated by a space. This means a bar connecting the -th and -th lines is added at the height between the -th bar from the top and the -th bar. If the bar goes above the uppermost bar, is 0. If it goes below the lowermost bar, is . If there are several valid positions, output the one with the smallest .
OK and NG are uppercase.