UVA - 12083 Guardian of Decency (匈牙利算法 求 最大独立集)

题意:

给你 n 个人的 性别 身高, 喜欢的运动和音乐。 告诉你一些两个人能在一起的条件, 问 最多多少人 在一起?

思路:

有性别之分。

可以建成一个二分图。

然后反向建边, 如果男i 和 女j 不能再一起 就连一条边。

然后这样, 二分图的最大独立集 就是 答案。 所有人都不连边, 所有人都可以在一起。

总数 - 最大匹配数 即是答案。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;


const int maxn = 500 + 7;
struct Node{
    int h;
    char sex[3];
    char music[102];
    char sport[102];
}p[maxn];
vector<int>g[maxn];

int aaa(int x){
    if (x < 0) return -x;
    return x;
}
bool check(int i,int j){
    if (p[i].sex[0] == p[j].sex[0]) return true;
    if (aaa(p[i].h - p[j].h) > 40) return true;
    if (strcmp(p[i].music, p[j].music)) return true;
    if (!strcmp(p[i].sport, p[j].sport)) return true;
    return false;
}

int vis[maxn];

int match[maxn];

bool hungry(int x){
    for (int i = 0; i < g[x].size(); ++i){
        int v = g[x][i];
        if (vis[v]) continue;
        vis[v] = 1;
        if (match[v] == -1 || hungry(match[v])){
            match[v] = x;
            return true;
        }
    }
    return false;
}


int solve(int n){
    int ans = 0;
    memset(match,-1,sizeof match);
    for (int i = 0; i < n; ++i){
        memset(vis,0,sizeof vis);
        if (hungry(i))++ans;
    }
    return ans;
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int n;
        scanf("%d",&n);
        for (int i = 0; i < n; ++i){
            g[i].clear();
            scanf("%d%s%s%s", &p[i].h, p[i].sex, p[i].music, p[i].sport);
        }

        for (int i = 0; i < n; ++i){
            if (p[i].sex[0] == 'M') continue;
            for (int j = 0; j < n; ++j){
                if (i == j) continue;
                if (!check(i, j)) g[i].push_back(j);
            }
        }
        int ans = solve(n);
        printf("%d\n", n - ans);

    }
    return 0;
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值