This page is still under construction.

Parts of this page are still being built. What you see may change.

Painting a Board

Time limit1sMemory limit128 MB

Summary
Given up to 15 rectangles with colors and vertical precedence constraints, find the minimum number of brush pick-ups to paint every rectangle.
Level

Hard8 of 10

Topics
Dynamic programming, Bit manipulation, Graph, Topological sort
Solved
No attempts yet

Problem

The CE Digital company has built an Automatic Painting Machine (APM) that paints a flat board. The board is completely covered by adjacent, non-overlapping rectangles of various sizes, and each rectangle has a single predefined color.

The APM has a set of brushes, one distinct color per brush. To paint the board, the machine repeatedly picks up a single brush of some color CC and, while holding it, paints one or more rectangles whose predefined color is CC. Painting obeys the following rules:

  • To keep the paints from leaking and colors from mixing, a rectangle may be painted only after every rectangle immediately above it has already been painted. (In Figure 1, rectangle FF can be painted only after rectangles CC and DD have been painted.)
  • Each rectangle must be painted in a single pass; partial painting of a rectangle is not allowed.

Write a program that paints the whole board while minimizing the number of brush pick-ups. If the same brush is picked up more than once, every pick-up is counted.

Input

The first line contains an integer MM, the number of test cases (1≤M≤101 \le M \le 10).

Each test case begins with a line containing an integer NN, the number of rectangles (1≤N≤151 \le N \le 15). The next NN lines each describe one rectangle with five integers:

y1x1y2x2cy_1 \quad x_1 \quad y_2 \quad x_2 \quad c

where (y1,x1)(y_1, x_1) is the upper-left corner and (y2,x2)(y_2, x_2) is the lower-right corner of the rectangle, and cc is its color code.

  • The color code cc is an integer with 1≤c≤201 \le c \le 20.
  • The upper-left corner of the board is always (0,0)(0, 0).
  • Every coordinate is in the range 0≤coordinate≤990 \le \text{coordinate} \le 99.

Output

For each test case, print a single line containing the minimum number of brush pick-ups.

Examples3

  1. Example 1

    Input
    1
    7
    0 0 2 2 1
    0 2 1 6 2
    2 0 4 2 1
    1 2 4 4 2
    1 4 3 6 1
    4 0 6 4 1
    3 4 6 6 2
    
    Expected output
    3
    
  2. Example 2

    Input
    1
    1
    0 0 5 5 3
    
    Expected output
    1
    
  3. Example 3

    Input
    1
    2
    0 0 3 3 1
    0 3 3 6 2
    
    Expected output
    2