Maximum matching of binary graphs
Hungarian algorithm templates
A mat array records matching objects a flag array record whether or not the current point has been matched
Flag array Remember that each time you empty the function part has a recursive process
Binary graph max matching km algorithm/
*
8 5 5
1 1
1 2
2 1
2 2
3 3
4 3
4 5
5 5
*
#include < iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
# include<algorithm>
#include <cmath>
#include <queue>
using namespace std;
int n,m;
int mp[1000][1000],flag[1000],mat[1000];
int km (int. x) {for
(int i=1;i<=m;i++)
{
if (Mp[x][i]&&!flag[i]) {
flag[i]=1;
if (!mat[i]| | KM (mat[i])) {
mat[i]=x;
return 1;}}
}
return 0;
}
int main () {
int p,x,y;
scanf ("%d%d%d", &p,&n,&m);
for (int i=1;i<=p;i++)
{
scanf ("%d%d", &x,&y);
mp[x][y]=1;
}
int sum=0;
for (int i=1;i<=n;i++)
{
memset (flag,0,sizeof (flag));
if (km (i)) sum++;
}
printf ("%d\n", sum);
return 0;
}