Going to the Movies
Time limit2sMemory limit128 MB
Given M movies each covering a subset of P preferences, find the smallest number of movies whose coverage includes all P preferences.
- Level
Medium5 of 10
- Topics
- Bit manipulation, Brute force, Greedy, Implementation
- Solved
- No attempts yet
Problem
Alan Turing and Edsger Dijkstra rarely get the chance to go to the movies. Being European, cinema is more of an American tradition to them, so on their infrequent visits to the States they gather with their colleagues every weekend and stay glued to the big screen until Monday. Finding a quick route to the theater is easy; agreeing on what to watch is not. Their tastes differ wildly. Turing, for instance, loves romance films, while Dijkstra can't stand them, which makes choosing movies extremely difficult. They refuse to split up and watch different titles alone, because then there would be no point in getting together as a group: they could not even discuss what they saw, since not everyone would have seen the same films.
To solve this, they devised a scheme to minimize the number of films they must watch while still satisfying every member. They aggregate everyone's tastes into a master list of preferences such as "romance", "action", and "horror". For each candidate movie they determine which preferences on the list it satisfies. All that is left is to find the smallest set of movies that together satisfy every preference on the list. That is where you come in.
Input
The first line contains the number of data sets. The data sets follow, each in the form below.
The first line of a data set contains two integers and : the number of movies and the number of preferences on the list, with and . The next lines describe the movies; the -th of these lines lists the preferences that movie satisfies, given as between and integers in the range to .
Output
For each data set, print "Data Set x:" on a line by itself, where is the data set's number, starting from . On the next line, print the minimum number of movies needed to satisfy every preference. If the preferences cannot all be satisfied, print "Impossible" instead. Separate consecutive data sets with a blank line.