#include <cstdio>
#include <iostream>
#include <cstring>
const int MAXN = 510;
int uN,vN;
int g[MAXN][MAXN];
int linker[MAXN];
bool used[MAXN];
bool dfs(int u)
{
for(int v = 1; v <= vN;v++)
if(g[u][v] && !used[v])
{
used[v] = true;
if(linker[v] == -1 || dfs(linker[v]))
{
linker[v] = u;
return true;
}
}
return false;
}
int hungary()
{
int res = 0;
memset(linker,-1,sizeof(linker));
for(int u = 1;u <= uN; u++)
{
memset(used,false,sizeof(used));
if(dfs(u))res++;
}
return res;
}
int main()
{
int n,m;
scanf("%d %d",&n,&m);
uN = vN = n;
for(int i = 0; i < m; i ++)
{
int u,v;
scanf("%d %d",&u,&v);
g[u][v] = 1;
}
int ans = hungary();
printf("%d\n",ans);
}
直接贴代码了
最小定点覆盖等于最大匹配