#C6596. Minimum Books to Cover Themes
Minimum Books to Cover Themes
Minimum Books to Cover Themes
You are given a collection of books, each associated with a set of themes, and a list of required themes. Your task is to determine the minimum number of books required to cover all the required themes. If it is impossible to cover all required themes with the given books, output impossible
.
This problem can be seen as a variant of the set cover problem. A valid solution selects a subset of the books such that the union of their themes contains every theme from the required list. Formally, if we denote by \(S\) the set of required themes and by \(B_i\) the set of themes covered by the ith book, you need to find the minimum \(k\) for which there exists a collection of \(k\) books satisfying:
\[ \bigcup_{i=1}^{k} B_{i} \supseteq S \]
If no such collection exists, output impossible
.
inputFormat
The input consists of multiple test cases. Each test case is formatted as follows:
- An integer \(m\) representing the number of books. If \(m = 0\), it indicates the end of input.
- \(m\) lines follow. Each line describes a book in the format:
Title k theme1 theme2 ... themek
whereTitle
is the name of the book, \(k\) is the number of themes associated with this book, followed by \(k\) themes. - An integer \(n\) representing the number of required themes.
- \(n\) lines follow, each containing a required theme.
Read from standard input.
outputFormat
For each test case, output a single line containing the minimum number of books needed to cover all the required themes, or impossible
if it cannot be achieved. Write your output to standard output.
4
Book1 2 Adventure Mystery
Book2 1 Romance
Book3 3 Adventure ScienceFiction Fantasy
Book4 2 Mystery Romance
3
Adventure
Mystery
Romance
0
2
</p>