#P1894. [USACO4.2] 完美的牛栏The Perfect Stall

[USACO4.2] 完美的牛栏The Perfect Stall

Description

Last week, Farmer John finished building his new barn, using the latest milking technology.

Unfortunately, due to construction issues, every stall is different.

In the first week, Farmer John let the cows enter the stalls at random, but problems quickly appeared: each cow will only produce milk in the stalls she likes.

Last week, Farmer John collected the cows’ preferences (for each cow, which stalls she likes to use).

A stall can hold only one cow, and of course, a cow can be assigned to at most one stall.

Given the cows’ preferences, compute the maximum assignment.

Input Format

The first line contains two integers, nn and mm. nn is the number of Farmer John’s cows, and mm is the number of stalls in the new barn.

Lines 22 through n+1n+1 (a total of nn lines) each describe one cow and contain several integers. The first number sis_i is the number of stalls this cow is willing to use. The next sis_i numbers are the indices of those stalls. Stall indices are in the range [1,m][1,m]. On the same line, no stall is listed twice.

Output Format

Output a single line with one integer, the maximum number of stalls that can be assigned.

5 5
2 2 5
3 2 3 4
2 1 5
3 1 2 5
1 2
4

Hint

0n,m2000 \le n,m \le 200, 0sim0 \le s_i \le m.

Translated by ChatGPT 5