Match children to toys they like, with a per-category cap on how many toys of each category can be used, and maximize satisfied children.
Medium7GraphBFSGreedyImplementationNo attempts yetTime limit2sMemory limit512 MBWaif Until Dark is a daycare center for households where both parents work during the day. To keep the children busy, the center has a set of toys they can play with. Some toys belong to one category, such as sports toys, musical toys, or dolls. To limit wear on those toys, the teachers allow only a fixed number of toys from each category during playtime. Children do not all like the same toys, so handing every child a toy they like is not always possible.
A child receives at most one toy, and a toy goes to at most one child. A child is satisfied when the child receives a toy from their own list. Find the largest number of children that can be satisfied at the same time.
The first line contains the number of children n, the number of toys m, and the number of toy categories p (1≤n,m≤100, 0≤p≤m). Children and toys are numbered starting at 1.
Each of the next n lines has the form k i1 i2 … ik (1≤k≤m, 1≤i1,i2,…,ik≤m). The j th of these lines means that child j is willing to play with toys i1 through ik.
Each of the next p lines has the form l t1 t2 … tl r (1≤r≤l≤m, 1≤t1,t2,…,tl≤m). The j th of these lines means that toys t1 through tl belong to category j and that at most r toys of that category can be used.
A toy belongs to at most one category. A toy that never appears on those p lines has no category, and all such toys can be used with no limit. No toy number appears twice on the same line.
Print the largest number of children that can be satisfied with a toy they like.