The Trough Game
InterviewTime limit1sMemory limit128 MB
Given N troughs and M queries that each count filled troughs within a listed subset, find the filled set or report impossible or non-unique.
- Level
Medium6 of 10
- Topics
- Brute force, Bit manipulation, Math
- Solved
- No attempts yet
Problem
Farmer John and Bessie are playing another game, this time with troughs of water.
Farmer John has hidden () troughs behind the barn and filled some of them with food. Bessie asks () questions, each of the form: “Of the troughs in this list, how many are filled?”
Help Bessie deduce exactly which troughs are filled.
For example, suppose there are four troughs and Bessie asks these four questions, receiving the answers shown:
- Troughs {1}: 1 filled
- Troughs {2, 3}: 1 filled
- Troughs {1, 4}: 1 filled
- Troughs {3, 4}: 1 filled
She can reason as follows:
- Question 1 says trough 1 is filled.
- With trough 1 filled, question 3 forces trough 4 to be empty.
- With trough 4 empty, question 4 forces trough 3 to be filled.
- With trough 3 filled, question 2 forces trough 2 to be empty.
So the troughs are filled exactly as 1 0 1 0.
Input
- Line 1: two space-separated integers and .
- Lines 2 to : each line describes one question. It is a string of contiguous characters, each 0 or 1 (a 1 marks a trough that belongs to the question’s list), followed by a space and a single integer giving how many troughs in that list are filled.
Output
Print a single line:
IMPOSSIBLEif no set of filled troughs is consistent with all of Farmer John’s answers.NOT UNIQUEif the answers are consistent but do not determine the filled troughs uniquely.- Otherwise, a string of contiguous 0/1 characters giving the unique set of filled troughs.