标签:最大匹配 c++ cpp size ems tin logs 输出 class
给定一个二分图,结点个数分别为n,m,边数为e,求二分图最大匹配数
1 1 1 1 1
1
#include<bits/stdc++.h>
#define maxn 2999
using namespace std;
int couple[maxn];
int book[maxn];
int Map[maxn][maxn];
int n,m,e;
int ans=0;
bool find(int x)
{
int i,j;
for(j=1;j<=m;j++)//扫描所有的妹子
{
if(Map[x][j]&&!book[j])
{
book[j]=1;
if(couple[j]==0||find(couple[j]))
{//没有归属,或者归属可以被抢走
couple[j]=x;
return 1;
}
}
}
return 0;
}
int main()
{
cin>>n>>m;
cin>>e;
int from,to;
for(int i=1;i<=e;i++)
{
cin>>from>>to;
if(to>m)
continue;
Map[from][to]=1;
}
for(int i=1;i<=n;i++)
{
memset(book,0,sizeof(book));
if(find(i))
ans++
}
cout<<ans<<endl;
return 0;
}
标签:最大匹配 c++ cpp size ems tin logs 输出 class
原文地址:http://www.cnblogs.com/foreverpiano/p/6906967.html