匈牙利算法模板题
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <map>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
#define PI acos(-1)
#define DEBUG(a) cout << (a) << endl
typedef long long ll;
int dir8[8][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {-1, 1}, {-1, -1}, {1, -1}, {1, 1}};
int dir4[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
const int INF = 0x3f3f3f3fLL;
const long long LLF = 0x3f3f3f3f3f3f3f3fLL;
const int MAXn = 1e5 + 15;
const int mod = 1e9 + 7;
int cnt,n,m,Next[400];
bool mapn[402][402],mark[400];
bool check(int x)
{
for (int i=1;i<=m;i++)
if (mapn[x][i]&&(!mark[i]))
{
mark[i]=true;
if (Next[i]==0||check(Next[i]))
{
Next[i]=x;
return true;
}
}
return false;
}
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
{
int a;
scanf("%d",&a);
for (int j=1;j<=a;j++)
{
int b;
scanf("%d",&b);
mapn[i][b]=true;
}
}
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
mark[j]=false;
if (check(i)) cnt++;
}
printf("%d\n",cnt);
return 0;
}