匈牙利算法模板(求最大匹配数)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define MAXN 1010100
#define LL long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll __int64
#define INF 0x7fffffff
#define cs(s) freopen(s,"r",stdin)
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-10
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
//head
vector<int>v[555];
int vis[555],ues[555],n,m,k,s,t;
int find(int x){
	for(auto k:v[x]){
		if(!ues[k]){
			ues[k]=1;
			if(!vis[k]||find(vis[k])){
				vis[k]=x;
				return 1;
			}
		}
	}
	return 0;
}
int match(){
	int sum=0;
	for(int i=1;i<=m;i++){
		memset(ues,0,sizeof(ues));
		if(find(i))sum++;
	}
	return sum;
}
int main(){
	ios::sync_with_stdio(false);
	while(cin>>k&&k){
		cin>>m>>n;
		for(int i=1;i<=m;i++)v[i].clear();
		memset(vis,0,sizeof(vis));
		for(int i=1;i<=k;i++){
			cin>>s>>t;
			v[s].pb(t);
		}
		cout<<match()<<endl;
	}
	return 0;
}

转载于:https://www.cnblogs.com/pubgoso/p/10759731.html

你可能感兴趣的:(匈牙利算法模板(求最大匹配数))