Milkshakes (Large)
InterviewTime limit5sMemory limit512 MB
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 flavors, and each flavor is prepared either malted or unmalted, so there are 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 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 , the number of test cases. Each test case is given in this format.
- A line with , the number of flavors.
- A line with , the number of customers.
- lines, one per customer. Each line starts with , the number of types that customer likes, followed by pairs of integers " " describing those types. is a flavor number from to , and is for unmalted or for malted.
Numbers on the same line are separated by single spaces.
Limits
- , 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 )
- The sum of over all customers of one test case is at most
Output
Print lines, one per test case in input order. Line starts with Case #X: and continues with one of the following.
IMPOSSIBLE, if the customers cannot all be satisfied.- Otherwise, space-separated integers, one for each flavor from to . A flavor prepared unmalted is , and a flavor prepared malted is .
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.