#K42717. Minimum Queries to Retrieve Target Items
Minimum Queries to Retrieve Target Items
Minimum Queries to Retrieve Target Items
You are given several test cases. In each test case, you are provided with a set of target items and a collection of queries, where each query returns a list of items. Your objective is to determine the minimum number of queries whose union contains all the target items.
More formally, let \(T\) be the set of target items, and let \(Q_1, Q_2, \dots, Q_n\) be the queries. You need to find the smallest integer \(k\) and a subset \(S\) of queries (with \(|S|=k\)) such that:
[ T \subseteq \bigcup_{q \in S} q ]
If no such subset exists, output -1
.
Note: You may assume that the queries are designed so that a brute force solution is feasible.
inputFormat
The input is read from stdin and has the following format:
T n1 m1 target1 target2 ... targetm1 k1,1 item item ... ... kn1,1 item item ... ... nT mT (... similarly for each test case ...)
Here, T
is the number of test cases. For each test case:
- The first line contains two integers:
n
(the number of queries) andm
(the number of target items). - The second line contains
m
strings representing the target items. - Each of the next
n
lines starts with an integer (the number of items returned by the query, which can be ignored) followed by the list of items for that query.
outputFormat
For each test case, print a single integer on a new line to stdout: the minimum number of queries needed to ensure that the union of the chosen queries covers all target items. If it is impossible, print -1
.
2
3 5
item1 item2 item3 item4 item5
3 item1 item3 item5
2 item2 item4
1 item3
2 3
item1 item2 item3
2 item1 item2
2 item2 item3
2
2
</p>