Programming Contests
Time limit1sMemory limit128 MB
Given problems that can each go to one of several named contests each needing a fixed count, find the maximum subset of contests that can be simultaneously fully staffed, requiring bitmask search combined with flow-style feasibility checks.
- Level
Hard8 of 10
- Topics
- Bit manipulation, Graph, Brute force, Combinatorics
- Solved
- No attempts yet
Problem
Every programming contest features a different kind of problem. For example, TopCoder caps the maximum array size at 50. Also, IOI is a contest that children take part in, so Suffix Tree problems do not appear.
Woohyun writes problems for distinct programming contests. Each contest needs a different number of problems. For instance, an ACM-ICPC-style contest usually needs 10 problems, while a TopCoder SRM needs 5.
Woohyun has prepared problems in advance and, for each problem, has decided which contests it may be used in.
Assuming every contest is to be held at the same time, determine the maximum number of contests that can actually run. To hold a contest, all of its required problems must be filled, and each problem may be used in at most one contest.
Input
The input consists of several test cases.
The first line of each test case contains the number of contests and the number of prepared problems (, ).
Each of the next lines contains a contest name and the number of problems that contest needs to run. A contest name consists of upper- and lower-case letters and digits, is at most 100 characters long, and is case-sensitive. The required problem count is at most 100.
Each of the next lines lists, separated by spaces, the names of the contests in which that problem may be used. If a problem cannot be used in any contest, its line is empty.
The last line of the input contains two 0s and is not processed.
Output
For each test case, print on one line the maximum number of contests that can be held simultaneously, together with the case number. If the answer for the -th test case is , print it in the format Case #k: c.