uva 12083 - Guardian of Decency(二分图匹配)

942 篇文章 2 订阅

题目链接:uva 12083 - Guardian of Decency


对于男女可以分成X和Y,矛盾的两个人之间建立一条边, 做二分图最大匹配,每条被选取的边表示只能有一人被选中。


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <map>
#include <string>
#include <algorithm>

using namespace std;
const int maxn = 505;

int N, L[maxn];
bool S[maxn], T[maxn];
vector<int> G[maxn];

int age[maxn], gender[maxn], music[maxn], sport[maxn], idP, idQ;
map<string, int> P, Q;

bool match(int u) {
	S[u] = true;
	for (int i = 0; i < G[u].size(); i++) {
		int j = G[u][i];
		if (!T[j]) {
			T[j] = true;
			if (!L[j] || match(L[j])) {
				L[j] = u;
				return true;
			}
		}
	}
	return false;
}

int KM () {
	int ret = 0;
	memset(L, 0, sizeof(L));

	for (int i = 1; i <= N; i++) {
		if (gender[i]) continue;
		memset(S, false, sizeof(S));
		memset(T, false, sizeof(T));
		if (match(i)) ret++;
	}
	return ret;
}

int getID(map<string,int>& M, int& n, char* s) {
	if (!M.count(s)) M[s] = n++;
	return M[s];
}

bool judge (int a, int b) {
	if (abs(age[a]-age[b]) > 40) return false;
	if (gender[a] == gender[b]) return false;
	if (music[a] != music[b]) return false;
	if (sport[a] == sport[b]) return false;
	return true;
}

int main () {
	int cas;
	scanf("%d", &cas);
	while (cas--) {
		scanf("%d", &N);
		idP = idQ = 0;
		P.clear(); Q.clear();

		char s1[maxn], s2[maxn], s3[maxn];
		for (int i = 1; i <= N; i++) {
			scanf("%d%s%s%s", &age[i], s1, s2, s3);
			gender[i] = (s1[0] == 'M');
			music[i] = getID(P, idP, s2);
			sport[i] = getID(Q, idQ, s3);
		}

		for (int i = 1; i <= N; i++) {
			if (gender[i]) continue;
			G[i].clear();
			for (int j = 1; j <= N; j++) if (i != j) {
				if (judge(i, j))
					G[i].push_back(j);
			}
		}

		int t = KM();
		printf("%d\n", N - t);
	}
	return 0;
}


  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值