二份答案 + 多重匹配
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#define INF (int)(1e9)
#define maxn 1010
using namespace std;
typedef long long ll;
int edge[maxn][maxn];
bool vis[maxn];
int uN, vN;//左集合顶点数目 右集合顶点数目
int vcy[maxn]; //vcy[i]表示右集合匹配到左集合的顶点数量
int cy[maxn][maxn]; //cy[i][j]表示右集合i顶点匹配的第j个元素
int limit; //右集合顶点做多匹配左集合顶点的个数
//寻找增广路径
bool dfs(int u) {
for (int i = 0; i < vN; ++ i) {
if (edge[u][i] && !vis[i]) {
vis[i] = 1;
if (vcy[i] < limit) {
cy[i][vcy[i]++] = u;
return true;
}
for (int j = 0; j < vcy[i]; ++ j) {
if (dfs(cy[i][j])) {
cy[i][j] = u;
return true;
}
}
}
}
return false;
}
//存在多重匹配
bool max_match() {
memset(vcy, 0, sizeof(vcy));
for (int i = 0; i < uN; ++ i) {
memset(vis, 0, sizeof(vis));
if (!dfs(i)) return false;
}
return true;
}
int main() {
int n, m;
while (scanf("%d%d", &n, &m) != EOF) {
if (n == 0 && m == 0) break;
char s[20];
int tmp;
memset(edge, 0, sizeof(edge));
for (int i = 0; i < n; ++ i) {
scanf("%s", s);
while (getchar() == ' ') {
scanf("%d", &tmp);
edge[i][tmp] = 1;
}
}
uN = n, vN = m;
int left = 0, right = uN;
while (left < right) {
limit = (left + right) >> 1;
if (max_match()) right = limit;
else left = limit + 1;
}
cout << left << endl;
}
}