uvalive3415 Guardian Of Decency --- 匈牙利算法

题解:根据性别建立二分图,把可能相爱的异性匹配在一起,题目意思是找尽量多的点,使得其中任意2点之间没有边,即寻找二分图的最大独立集,最大独立集 = 点数 - 最大匹配数(最小点覆盖数),所以直接匈牙利算法找最大匹配数即可

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 510;
const int maxc = 110;
struct Node {
    int height;
    char gender[3];
    char music[maxc];
    char sport[maxc];
} stu[maxn];
int n;
int G[maxn][maxn];
bool mark[maxn];
int match[maxn];

bool KM(int u) {
    for(int v = 0;v < n;v++) {
        if(G[u][v] == 0) continue;
        if(stu[v].gender[0]!='F') continue;
        if(mark[v]) continue;
        mark[v] = 1;
        if(match[v]==-1||KM(match[v])) {
            match[v] = u;
            return true;
        }
    }

    return false;
}

int main() {
   // freopen("out.txt","w",stdout);
    int T;
    scanf("%d",&T);
    while(T--) {
        memset(match,-1,sizeof(match));
        scanf("%d",&n);
        for(int i = 0;i < n;i++) {
            scanf("%d%s%s%s",&stu[i].height,stu[i].gender,stu[i].music,stu[i].sport);
        }
        memset(G,0,sizeof(G));
        for(int i = 0;i < n;i++)
            for(int j = i+1;j < n;j++) {
                if((strcmp(stu[i].gender,stu[j].gender)!=0))
                    if((abs(stu[i].height-stu[j].height)<=40)
                        && (strcmp(stu[i].music,stu[j].music)==0)
                        && (strcmp(stu[i].sport,stu[j].sport)!=0 )) {
                            G[i][j] = G[j][i] = 1;
                        }
            }
        
        int ans = 0;
        for(int i = 0;i < n;i++) {
            memset(mark,0,sizeof(mark));
            if(strcmp(stu[i].gender,"M") == 0)
                if(KM(i)) ans++;
        }
        printf("%d\n",n-ans);

    }

        
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值