Joining Points

Time limit1sMemory limit128 MB

Summary
Given two sets of colored points in general position inside a square, output a non-crossing spanning tree for each color separately.
Level

Hard8 of 10

Topics
Geometry, Greedy, Sorting, Divide and conquer
Solved
No attempts yet

Problem

"Joining Points" is a single-player game. Choose two integers greater than 2 and call them gg and rr. Draw four points at the vertices of a square: the top two are green and the bottom two are red. Place more green and red points inside the square so that no three points, including the four corners, lie on one line. Stop when there are gg green points and rr red points in total.

After the board is ready, join points with line segments. You may connect two points when:

  • the two points share the same color, and
  • the segment between them does not cross any previously drawn segment except at endpoints.

Points uu and vv are in the same component when you can travel from uu to vv using segments already drawn.

You win by connecting all green points into one component with exactly g−1g-1 green segments, and all red points into another component with exactly r−1r-1 red segments. When the points are placed as described, a winning construction always exists.

You receive a square board of side length ss with gg green points and rr red points at integer coordinates (xi,yi)(x_i, y_i). Green points are numbered 1 through gg: point 1 is at (0,s)(0,s), point 2 at (s,s)(s,s), and interior points are numbered 3 through gg. Red points are numbered 1 through rr: point 1 is at (0,0)(0,0), point 2 at (s,0)(s,0), and interior points are numbered 3 through rr.

The figure shows one valid finish: all green points in one component and all red points in another. No three points are collinear, and segments meet only at endpoints.

Given the coordinates of all green and red points, output how to draw g−1g-1 green segments and r−1r-1 red segments that connect each color into a single component without crossing segments.

Input

  • Line 1: integer gg.
  • Next gg lines: two space-separated integers xi,yix_i, y_i for green points 1 through gg.
  • Line g+2g+2: integer rr.
  • Next rr lines: two space-separated integers xi,yix_i, y_i for red points 1 through rr.

Output

Print (g−1)+(r−1)(g-1)+(r-1) lines, one per drawn segment.

Each line contains two space-separated integers and one character. The integers are the numbers of the joined points; the character is g for green or r for red.

The order of lines and the order of endpoints within a line do not matter.

Constraints

  • 3≤g≤50 0003 \le g \le 50\,000: number of green points.
  • 3≤r≤50 0003 \le r \le 50\,000: number of red points.
  • 0<s≤200 000 0000 < s \le 200\,000\,000.

Examples6

  1. Example 1

    Input
    6
    0 1000
    1000 1000
    203 601
    449 212
    620 837
    708 537
    8
    0 0
    1000 0
    185 300
    314 888
    416 458
    614 622
    683 95
    838 400
    
    Expected output
    1 2 g
    1 2 r
    1 3 g
    3 4 g
    1 7 r
    1 3 r
    2 5 r
    2 8 r
    8 6 r
    6 4 r
    2 5 g
    2 6 g
    
  2. Example 2

    Input
    3
    0 1000
    1000 1000
    596 868
    5
    0 0
    1000 0
    200 931
    989 553
    947 368
    
    Expected output
    1 2 g
    1 2 r
    2 5 r
    2 4 r
    5 3 r
    2 3 g
    
  3. Example 3

    Input
    5
    0 1000
    1000 1000
    783 395
    661 481
    420 895
    6
    0 0
    1000 0
    203 201
    693 408
    993 58
    439 454
    
    Expected output
    1 2 g
    1 2 r
    1 3 r
    1 6 r
    2 4 r
    2 3 g
    2 5 r
    1 4 g
    1 5 g
    
  4. Example 4

    Input
    5
    0 1000
    1000 1000
    33 246
    993 759
    200 95
    6
    0 0
    1000 0
    580 970
    639 265
    581 529
    408 162
    
    Expected output
    1 2 g
    1 2 r
    1 3 g
    3 5 g
    2 4 r
    2 6 r
    2 3 r
    2 4 g
    2 5 r
    
  5. Example 5

    Input
    6
    0 1000
    1000 1000
    90 954
    416 913
    4 531
    96 699
    5
    0 0
    1000 0
    612 98
    341 138
    406 988
    
    Expected output
    1 2 g
    1 2 r
    1 5 g
    2 3 r
    2 4 r
    1 6 g
    2 5 r
    1 3 g
    1 4 g
    
  6. Example 6

    Input
    6
    0 1000
    1000 1000
    154 264
    865 307
    288 347
    719 482
    5
    0 0
    1000 0
    301 547
    815 547
    661 359
    
    Expected output
    1 2 g
    1 2 r
    1 3 g
    2 3 r
    3 5 g
    2 5 r
    2 4 g
    5 4 r
    1 6 g