#P2961. Fair Cookie Distribution
Fair Cookie Distribution
Fair Cookie Distribution
Farmer John's N cows (1 ≤ N ≤ 1000) numbered 1..N have formed M (1 ≤ M ≤ 100) study groups. In group \(G_i\), there are \(S_i\) (1 ≤ \(S_i\) ≤ 19) cows, and a cow may participate in more than one group.
For each study group meeting, one cow from that group is chosen to bring cookies. Because cookies are costly and time‐consuming to obtain, the cows want the duty to be shared as fairly as possible. In particular, if a cow participates in groups of sizes \(c_1, c_2, \ldots, c_K\) (i.e. she attends meetings having \(c_j\) members), then she is willing to bring cookies in at most \[ \Bigl\lceil \frac{1}{c_1} + \frac{1}{c_2} + \cdots + \frac{1}{c_K} \Bigr\rceil \] meetings.
Your task is to assign one cow to each group such that no cow is assigned more times than her limit. If such an assignment is impossible, output -1
. If more than one valid solution exists, you may output any one.
inputFormat
The first line contains two integers N and M.
Each of the next M lines starts with an integer \(S_i\) followed by \(S_i\) integers representing the cow numbers in group \(G_i\>.
outputFormat
If a valid assignment exists, output M integers where the i-th integer is the number of the cow assigned to group \(G_i\).
If no valid assignment exists, output -1
.
sample
3 2
2 1 2
2 2 3
1 3