#P6868. Non‐Intersecting Axis‐Aligned Pairing
Non‐Intersecting Axis‐Aligned Pairing
Non‐Intersecting Axis‐Aligned Pairing
You are given n integer points on the 2D plane. It is guaranteed that for every integer a, there are at most two points of the form \( (a, x) \), and for every integer b, at most two points of the form \( (x, b) \).
You need to connect these n points with exactly \( \frac{n}{2} \) line segments so that every point is an endpoint of exactly one segment. Each segment must be either horizontal or vertical, and no two segments intersect (i.e. they do not share any common point in their interiors).
Determine whether such a pairing is possible. If it is, output YES
followed by any valid set of segments. Otherwise, output NO
.
Note: Two segments (one horizontal and one vertical) intersect if the vertical segment's x-coordinate lies strictly between the horizontal segment's endpoints, and the horizontal segment's y-coordinate lies strictly between the vertical segment's endpoints.
inputFormat
The first line contains an even integer n
denoting the number of points. Each of the next n
lines contains two integers, representing the x
and y
coordinates of a point. It is guaranteed that for any integer a there are at most two points with x = a
and for any integer b there are at most two points with y = b
.
outputFormat
If a valid pairing exists, print YES
on the first line. Then print exactly \( \frac{n}{2} \) lines, each containing four integers x1 y1 x2 y2
representing the endpoints of a segment. The segments must be either horizontal (\( y1=y2 \)) or vertical (\( x1=x2 \)) and must not intersect. If no valid pairing exists, print NO
.
sample
4
0 0
0 1
1 0
1 1
YES
0 0 0 1
1 0 1 1
</p>