Milkshakes (Small)
InterviewTime limit5sMemory limit512 MB
Assign each flavor malted or unmalted so every customer gets a liked type, minimizing malted batches, with at most one malted liked type per customer.
- Level
Medium5 of 10
- Topics
- Greedy, Implementation, Brute force, Math
- Solved
- No attempts yet
Problem
You run a milkshake shop. You can prepare different flavors, and each flavor can be prepared malted or unmalted, so there are different types of milkshake.
Each customer has a set of milkshake types they like, and a customer is satisfied if you have prepared at least one type from that set. At most one of the types a customer likes is malted.
You want to make batches of milkshakes so that all of the following hold.
- There is exactly one batch for each flavor, and that batch is either malted or unmalted.
- For each customer, you make at least one type that the customer likes.
- The number of malted batches is as small as possible.
Decide whether you can satisfy every customer, and if you can, find which types you should make.
When every customer can be satisfied, the assignment that minimizes the number of malted batches is unique.
Input
The first line contains the number of test cases .
Each test case is given as follows.
- The first line contains the number of milkshake flavors .
- The second line contains the number of customers .
- Each of the next lines describes one customer. The line starts with the number of types that customer likes, , followed by integer pairs . Here is a flavor number between and , and is for unmalted or for malted.
All numbers on a line are separated by single spaces.
Limits
- Within one customer, the same pair never appears twice.
- Among the types one customer likes, at most one pair has .
Output
Print lines, one per test case, in the order the test cases are given. Each line starts with Case #X: , where is the test case number starting from . After that prefix, print the following.
- Print
IMPOSSIBLEif the customers' preferences cannot all be satisfied. - Otherwise print integers separated by spaces, one for each flavor from to . The integer is if that flavor should be prepared unmalted and if it should be prepared malted.
Hint
In the first test case of the first example, flavor must be malted to satisfy the first customer. Every other flavor can be unmalted. The second customer is satisfied by unmalted flavor , and the third customer is satisfied by unmalted flavor .
The second test case has only one flavor. One customer likes it malted and the other likes it unmalted, so you cannot satisfy both.