二分图匹配

死了,还有10天

十分模板的匈牙利(为什么大家都写hungary()???)

难道不应该是xiongyali()吗

hdu2063

模板题。背景还蛮有意思的。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
inline void in(int &p,char c=getchar())
{
	while(c<'0' or c>'9')
		c=getchar();
	p=0;
	while(c>='0' and c<='9')
		p=p*10+c-'0',c=getchar();
}
int k,girl,boy;
vector <int> w[501];
int ans;
bool incp[501];
int match[501];//match[boy]=girl
bool findpath(int x)
{
	for(unsigned int i=0;i<w[x].size();i++)
	{
		if(!incp[w[x][i]])
		{
			incp[w[x][i]]=1;
			if(!match[w[x][i]] or findpath(match[w[x][i]]))
			{
				match[w[x][i]]=x;
				return 1;
			}
		}
	}
	return 0;
}
void hungary()
{
	for(int i=1;i<=girl;i++)
	{
		memset(incp,0,sizeof(incp));
		if(findpath(i))
			ans++;
	}
}
int main()
{
	while(scanf("%d",&k) and k)
	{
		in(girl);in(boy);
		for(int i=1;i<=girl;i++)
			w[i].clear();
		memset(match,0,sizeof(match));
		ans=0;
		for(int i=0;i<k;i++)
		{
			int x,y;
			in(x);in(y);
			w[x].push_back(y);
		}
		hungary();
		cout<<ans<<endl;
	}
	return 0;
}
posted @ 2017-11-01 21:39  syhien  阅读(94)  评论(1编辑  收藏  举报