FZU 2039 Pets(匈牙利算法)

题意:商店有m只宠物,有n个人分别对应不喜欢某个宠物,问最多能卖出去几只宠物

简单的二分匹配

#include <iostream>
#include <cstring>
using namespace std;

int a[105][105];
int n, m, e;
bool used[105];
int belong[105];


bool find(int now){
	for (int i = 1; i <= n; i++){
		if (a[i][now] == 1 && used[i] == false){
			used[i] = true;
			if (belong[i] == 0 || find(belong[i])){
				belong[i] = now;
				return true;
			}
		}
	}
	return false;
}

int main()
{
	int t;
	cin >> t;
	for (int kase = 1; kase <= t; kase++){
		cin >> n >> m >> e;
		for (int i = 0; i < 105; i++){
			for (int j = 0; j < 105; j++)a[i][j] = 1;
		}

		for (int i = 1; i <= e; i++){
			int x, y;
			cin >> x >> y;
			a[x][y] = 0;
		}

		int ans = 0;
		memset(belong, 0, sizeof(belong));
		for (int i = 1; i <= m; i++){
			memset(used, false, sizeof(used));
			if (find(i))ans++;
		}
		cout << "Case " << kase << ":" << ' ' << ans << endl;
	}
	return 0;
}



你可能感兴趣的:(FZU 2039 Pets(匈牙利算法))