#graph602. 试题库

试题库

Problem Library

Problem Source

Network Flow 24 Problems

Problem Description

Suppose a problem library contains nn problems. Each problem is tagged with one or more categories. The same problem may belong to several categories. Now you must pick mm problems from the library to form an exam paper, and the paper must contain the required number of problems from each specified category. Design an algorithm that produces a paper satisfying the requirements.

Input Format

The first line contains two integers k,n(1≤n≤1000,2≤k≤20,k≤n)k, n(1\leq n\leq 1000, 2\leq k\leq 20, k\leq n). kk is the total number of categories in the library, nn is the total number of problems. The second line contains kk positive integers; the ii-th positive integer is the number of type-ii problems to be selected. The sum of these kk numbers is the total number mm of problems to be selected.

The following nn lines give the category information of each problem in the library. The first positive integer pp on each line indicates that the problem may belong to pp categories, and the following pp numbers are the category numbers to which the problem belongs.

Output Format

For the ii-th category, output i: followed by the indices of the problems of type ii. If there are multiple valid schemes, output any one of them. If the problem has no solution, output No Solution!.

Note: if the number of problems of a category is 00, you still need to put a space after i:.

Sample Input

3 15
3 3 4
2 1 2
1 3
1 3
1 3
1 3
3 1 2 3
2 2 3
2 1 3
1 2
1 2
2 1 2
2 1 3
2 1 2
1 1
3 1 2 3

Sample Output

1: 1 6 8
2: 7 9 10
3: 2 3 4 5

Hint

  • This is a classic bipartite-matching / maximum-flow modeling problem: connect a source to each category with capacity equal to the required count, each category to the problems that belong to it with capacity 1, and each problem to the sink with capacity 1. If the maximum flow equals the total required mm, the matching tells you which problems fill which categories.