This page is still under construction.

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

Milkshakes (Large)

Interview

Time limit5sMemory limit512 MB

Summary
Assign each of N flavors malted or unmalted so every customer gets a liked type, using the fewest malted batches, or report IMPOSSIBLE.
Level

Medium4 of 10

Topics
Greedy, Implementation
Solved
No attempts yet

Problem

You run a milkshake shop. You can prepare NN flavors, and each flavor is prepared either malted or unmalted, so there are 2N2N possible milkshake types.

Every customer has a set of types they like. That customer is satisfied if you prepare at least one type from the set. At most one of the types a customer likes is malted.

You prepare NN batches under these conditions.

  • You make exactly one batch per flavor, and that batch is either malted or unmalted.
  • Every customer receives at least one type they like.
  • The number of malted batches is as small as possible.

Decide whether all customers can be satisfied, and if they can, report which types to prepare. When they can be satisfied, exactly one choice minimizes the number of malted batches.

Input

The first line has an integer CC, the number of test cases. Each test case is given in this format.

  • A line with NN, the number of flavors.
  • A line with MM, the number of customers.
  • MM lines, one per customer. Each line starts with TT, the number of types that customer likes, followed by TT pairs of integers "XX YY" describing those types. XX is a flavor number from 11 to NN, and YY is 00 for unmalted or 11 for malted.

Numbers on the same line are separated by single spaces.

Limits

  • 1≤C≤51 \le C \le 5
  • 1≤N≤20001 \le N \le 2000
  • 1≤M≤20001 \le M \le 2000
  • T≥1T \ge 1, and no pair appears twice for a single customer
  • At most one of the types a customer likes is malted (at most one pair with Y=1Y = 1)
  • The sum of TT over all customers of one test case is at most 30003000

Output

Print CC lines, one per test case in input order. Line XX starts with Case #X: and continues with one of the following.

  • IMPOSSIBLE, if the customers cannot all be satisfied.
  • Otherwise, NN space-separated integers, one for each flavor from 11 to NN. A flavor prepared unmalted is 00, and a flavor prepared malted is 11.

Notes

In the first test case of the first example, the first customer forces flavor 1 to be malted. Every other flavor stays unmalted. The second customer is satisfied by flavor 2 unmalted, and the third customer by flavor 5 unmalted.

In the second test case there is only one flavor. One customer wants it malted and the other wants it unmalted, so the two cannot both be satisfied.

Examples5

  1. Example 1

    Input
    2
    5
    3
    1 1 1
    2 1 0 2 0
    1 5 0
    1
    2
    1 1 0
    1 1 1
    
    Expected output
    Case #1: 1 0 0 0 0
    Case #2: IMPOSSIBLE
    
  2. Example 2

    Input
    1
    1
    1
    1 1 0
    
    Expected output
    Case #1: 0
    
  3. Example 3

    Input
    1
    1
    1
    1 1 1
    
    Expected output
    Case #1: 1
    
  4. Example 4

    Input
    1
    4
    4
    1 1 1
    2 1 0 2 1
    2 2 0 3 1
    2 3 0 4 1
    
    Expected output
    Case #1: 1 1 1 1
    
  5. Example 5

    Input
    5
    3
    2
    2 1 0 3 0
    1 2 1
    2
    3
    1 1 1
    1 2 1
    2 1 0 2 0
    6
    4
    1 6 1
    2 6 0 1 1
    2 1 0 2 0
    1 3 0
    1
    1
    2 1 0 1 1
    4
    4
    1 2 1
    2 2 0 4 1
    2 4 0 1 0
    1 3 0
    
    Expected output
    Case #1: 0 1 0
    Case #2: IMPOSSIBLE
    Case #3: 1 0 0 0 0 1
    Case #4: 0
    Case #5: 0 1 0 1