Under construction

Still being built. Things may change

Who Brings the Cookies?

Time limit1sMemory limit128 MB

Summary
Assign one cookie-bringing cow per study group so each cow's meeting count stays within a ceiling-of-reciprocals limit, choosing the lexicographically smallest assignment.
Level

Hard8 of 10

Topics
Math, Greedy, Brute force, Implementation
Solved
No attempts yet

Problem

Farmer John's NN cows, numbered 11 through NN, form MM study groups. Study group ii has SiS_i member cows (a single cow may belong to several study groups).

For every study group, exactly one of its member cows must be chosen to bring cookies to that group's meeting. Cookies are expensive and take effort to prepare, so the cows want to share the burden as evenly as possible.

Suppose a cow belongs to study groups whose sizes are c1,c2,…,cKc_1, c_2, \dots, c_K (that is, she is a member of KK groups, and the jj-th of them has cjc_j members in total). Then she is willing to bring cookies to at most

⌈1c1+1c2+⋯+1cK⌉\left\lceil \frac{1}{c_1} + \frac{1}{c_2} + \cdots + \frac{1}{c_K} \right\rceil

meetings.

Assign to each study group the one cow that will bring its cookies, so that no cow is assigned more meetings than she is willing to bring.

Constraints: 1≤N≤10001 \le N \le 1000, 1≤M≤1001 \le M \le 100, and 1≤Si≤191 \le S_i \le 19.

Input

  • Line 1: two space-separated integers NN and MM.
  • Lines 2 to M+1M+1: line i+1i+1 describes study group ii as SiS_i followed by the SiS_i member cow numbers Gi,1,Gi,2,…,Gi,SiG_{i,1}, G_{i,2}, \dots, G_{i,S_i}, all space-separated.

Output

Print MM lines. Line ii contains aia_i, the number of the cow who brings cookies to study group ii.

Several valid assignments may exist, so output the lexicographically smallest one: make a1a_1 as small as possible; among all assignments with that a1a_1, make a2a_2 as small as possible; and so on.

If no valid assignment exists, print a single line containing −1-1 instead.

Hint

The limit counts how expensive a cow's meetings are: a member of a large group contributes little (its cost 1/c1/c is small), so she can afford more meetings.

For instance, take a cow that belongs to groups of sizes 22, 33, and 11. Her limit is ⌈12+13+11⌉=⌈1.833…⌉=2\left\lceil \tfrac{1}{2} + \tfrac{1}{3} + \tfrac{1}{1} \right\rceil = \lceil 1.833\ldots \rceil = 2, so she is willing to bring cookies to at most 22 meetings. A cow in two groups of size 33 has limit ⌈13+13⌉=1\left\lceil \tfrac{1}{3} + \tfrac{1}{3} \right\rceil = 1.

Examples3

  1. Example 1

    Input
    5 6
    3 2 4 5
    2 1 3
    3 1 2 3
    1 1
    2 2 5
    3 2 3 4
    
    Expected output
    2
    1
    2
    1
    5
    3
    
  2. Example 2

    Input
    1 1
    1 1
    
    Expected output
    1
    
  3. Example 3

    Input
    3 1
    3 1 2 3
    
    Expected output
    1