[Note]: Binary graph maximum matching Hungarian algorithm

Source: Internet
Author: User

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;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.