And Then, How Many Are There?

Time limit5sMemory limit128 MB

Summary
Given stacked discs of four colors, repeatedly remove two same-colored discs that are both uncovered; find the maximum number of discs removable.
Level

Medium7 of 10

Topics
Dynamic programming, Bit manipulation, Geometry, Sorting
Solved
No attempts yet

Problem

Mr. Solitarius, a famous creator of solo-play games, comes up with a new idea almost every day. His latest game uses discs of various colors and sizes.

At the start, all the discs are randomly scattered around the center of a table. During play you may remove a pair of discs of the same color, provided that neither of them has any disc lying on top of it. Note that a disc is not considered to lie on top of another when the two are only externally tangent.

Seven discs on the table

For example, in the figure above you can first remove the two black discs; doing so then makes it possible to remove the two white discs. The two gray discs, on the other hand, can never be removed.

Given the colors, sizes, and initial positions of the discs, compute the maximum number of discs that can be removed.

Input

The input consists of several datasets. Each dataset describes the state of a game immediately after all the discs have been scattered, in the following format:

n
x1 y1 r1 c1
x2 y2 r2 c2
...
xn yn rn cn

The first line contains a positive integer nn, the number of discs. Each of the next nn lines contains four space-separated integers describing one disc:

  • (xi,yi)(x_i, y_i) is the center, rir_i the radius, and cic_i the color index of the ii-th disc.
  • Whenever the ii-th disc lies on top of the jj-th disc, i<ji < j holds.

Every color index is an integer between 11 and 44 inclusive, and at most 66 discs in a dataset share the same color. Every center coordinate is between 00 and 100100 inclusive, and every radius is between 11 and 100100 inclusive.

The end of the input is indicated by a line containing a single zero.

Output

For each dataset, print a single line containing one integer: the maximum number of discs that can be removed.

Examples1

  1. Example 1

    Input
    4
    0 0 50 1
    0 0 50 2
    100 0 50 1
    0 0 100 2
    7
    12 40 8 1
    10 40 10 2
    30 40 10 2
    10 10 10 1
    20 10 9 3
    30 10 8 3
    40 10 7 3
    2
    0 0 100 1
    100 32 5 1
    0
    
    Expected output
    2
    4
    0