模版题,wa了一次因为忘记每次都初始化vis数组了。
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#define LL long long
#define FOR(i, x, y) for(int i=x;i<=y;i++)
using namespace std;
const int MAXN = 200 + 10;
int G[MAXN][MAXN];
int vis[MAXN];
int match[MAXN];
int n, m;
int path(int u)
{
for(int v=1;v<=m;v++)
{
if(G[u][v] && !vis[v])
{
vis[v] = 1;
if(match[v] == -1 || path(match[v]))
{
match[v] = u;
return 1;
}
}
}
return 0;
}
int main()
{
while(scanf("%d%d", &n, &m)!=EOF)
{
memset(G, 0, sizeof(G));
for(int i=1;i<=n;i++)
{
int x;
scanf("%d", &x);
while(x--)
{
int t;
scanf("%d", &t);
G[i][t] = 1;
}
}
memset(match, -1, sizeof(match));
int ans = 0;
for(int i=1;i<=n;i++)
{
memset(vis, 0, sizeof(vis));
ans += path(i);
}
printf("%d\n", ans);
}
return 0;
}