匈牙利算法,求教
查看原帖
匈牙利算法,求教
143742
Nelson_Wang楼主2021/9/28 17:53
#include <bits/stdc++.h>
using namespace std;
const int N = 505;
const int E = 5e4+5;

int n, m, ei;
int e[E*2], nex[E*2], hed[N*2], ct;
int f[N*2], match[N*2];

bool path(int x){
	for(int i=hed[x]; i; i=nex[i])
		if(!f[e[i]]){
			f[e[i]] = 1;
			if(!match[e[i]] || path(match[e[i]])){
				match[e[i]] = x;
				return 1;
			}
		}
	return 0;
}
int main(){
	// freopen("input.txt", "r", stdin);
	scanf("%d%d%d", &n, &m, &ei);
	for(int i=1; i<=ei; ++i){
		int a, b;
		scanf("%d%d", &a, &b);
		b = b+n;
		e[++ct] = b, nex[ct] = hed[a], hed[a] = ct;
		e[++ct] = a, nex[ct] = hed[b], hed[b] = ct;
	}
	int ans = 0;
	for(int i=1; i<=n+m; ++i){
		memset(f, 0, sizeof f);
		if(path(i))
			++ans;
	}
	printf("%d", ans);
	return 0;
}

2021/9/28 17:53
加载中...