Let be the number of vertices of color . Requiring at most covered vertices of that color is equivalent to requiring at least uncovered vertices.
For each color , add dummy vertices and connect each of them to every original vertex of color . Keep all original edges. Let and call the new graph . The answer is , where is the maximum matching size of .
Take any feasible original matching . For every color , choose uncovered vertices and match them to the dummy vertices of that color. This gives a matching of size in . Writing the original optimum as , we obtain .
Conversely, a maximum matching of can be modified to cover every dummy. Suppose a dummy of color is uncovered. At most original vertices of that color are matched to dummies, so some original vertex of color is not matched to a dummy. If is uncovered, adding contradicts maximality of the matching size. Hence is matched to an original vertex . Replace by . The size is unchanged, and the number of covered dummies increases by one. Repetition covers every dummy. We do not need to assume that an arbitrary maximum matching already covers them all.
Removing its dummy edges leaves original edges. At most vertices of color are covered by these edges, so this is feasible. Thus , proving the formula.
For subtask 1, choose an unprocessed vertex and enumerate leaving it uncovered or matching it to any remaining neighbor. Decrease the remaining capacities when selecting an edge; an edge with two endpoints of the same color consumes two units of that color. The number of matchings of a complete graph satisfies and . An implementation suffices since .
For subtask 2, apply Edmonds' blossom algorithm to . Odd cycles must be handled, so an ordinary bipartite matching algorithm cannot be applied directly. Since and , there are at most vertices. Time is and space is .
There are at most 600 transformed vertices, 44,850 original edges, 90,000 dummy edges, and 134,850 total edges. The transformed matching size is at most 300 and the original answer is at most 150. Also and . All integer calculations fit signed 64-bit integers. An absent color or a color with has .