题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=2063
基础二分图匹配入门题,算法详解见http://blog.csdn.net/liwen_7/article/details/7797652
代码:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=555;
bool g[maxn][maxn];
bool vis[maxn];
int p[maxn];
int k,n,m;
bool find_path(int x)
{
for(int i=1;i<=n;i++)
{
if(!vis[i]&&g[x][i])
{
vis[i]=1;
if(!p[i]||find_path(p[i]))
{
p[i]=x;
return 1;
}
}
}
return 0;
}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>k;
while(k)
{
memset(p,0,sizeof(p));
memset(g,0,sizeof(g));
cin>>m>>n;//m nvsheng
for(int i=1;i<=k;i++)
{
int x,y;
cin>>x>>y;
g[x][y]=1;
}
int tot=0;
for(int i=1;i<=m;i++)
{
memset(vis,0,sizeof(vis));
if(find_path(i))tot++;
}
cout<<tot<<endl;
cin>>k;
}
return 0;
}