#P13458. [GCJ 2008 #1A] Milkshakes

[GCJ 2008 #1A] Milkshakes

题目描述

You own a milkshake shop. There are NN different flavors that you can prepare, and each flavor can be prepared "malted" or "unmalted". So, you can make 2N2N different types of milkshakes.

Each of your customers has a set of milkshake types that they like, and they will be satisfied if you have at least one of those types prepared. At most one of the types a customer likes will be a "malted" flavor.

You want to make NN batches of milkshakes, so that:

  • There is exactly one batch for each flavor of milkshake, and it is either malted or unmalted.
  • For each customer, you make at least one milkshake type that they like.
  • The minimum possible number of batches are malted.

Find whether it is possible to satisfy all your customers given these constraints, and if it is, what milkshake types you should make.

If it is possible to satisfy all your customers, there will be only one answer which minimizes the number of malted batches.

输入格式

One line containing an integer CC, the number of test cases in the input file.

For each test case, there will be:

  • One line containing the integer NN, the number of milkshake flavors.
  • One line containing the integer MM, the number of customers.
  • MM lines, one for each customer, each containing:
    • An integer T1T \geq 1, the number of milkshake types the customer likes, followed by
    • TT pairs of integers "X Y", one for each type the customer likes, where XX is the milkshake flavor between 11 and NN inclusive, and YY is either 00 to indicate unmalted, or 11 to indicated malted.

Note that:

  • No pair will occur more than once for a single customer.
  • Each customer will have at least one flavor that they like (T1T \geq 1).
  • Each customer will like at most one malted flavor. (At most one pair for each customer has Y=1Y = 1).

All of these numbers are separated by single spaces.

输出格式

CC lines, one for each test case in the order they occur in the input file, each containing the string "Case #XX: " where XX is the number of the test case, starting from 11, followed by:

  • The string "IMPOSSIBLE", if the customers' preferences cannot be satisfied; OR
  • NN space-separated integers, one for each flavor from 11 to NN, which are 00 if the corresponding flavor should be prepared unmalted, and 11 if it should be malted.
2
5
3
1 1 1
2 1 0 2 0
1 5 0
1
2
1 1 0
1 1 1
Case #1: 1 0 0 0 0
Case #2: IMPOSSIBLE

提示

Sample Explanation

In the first case, you must make flavor #1 malted, to satisfy the first customer. Every other flavor can be unmalted. The second customer is satisfied by getting flavor #2 unmalted, and the third customer is satisfied by getting flavor #5 unmalted.

In the second case, there is only one flavor. One of your customers wants it malted and one wants it unmalted. You cannot satisfy them both.

Limits

Small dataset (10 Pts, Test set 1 - Visible)

  • C=100C = 100
  • 1N101 \leq N \leq 10
  • 1M1001 \leq M \leq 100

Large dataset (25 Pts, Test set 2 - Hidden)

  • C=5C = 5
  • 1N20001 \leq N \leq 2000
  • 1M20001 \leq M \leq 2000

The sum of all the TT values for the customers in a test case will not exceed 30003000.