UVALive 3415 Guardian of Decency(最大独立集)

题目链接:
UVALive 3415 Guardian of Decency
题意:
刘汝佳的大白书P356页上的题目.
最大独立集.


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <cassert>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
typedef long long ll;
const int MAX_N = 510;

int T, n, m, K, total;
int head[MAX_N * MAX_N], vis[MAX_N], match[MAX_N];

struct Student{
    int height;
    string sex, music, game;
}boy[MAX_N], girl[MAX_N];

struct Edge{
    int to,next;
}edge[MAX_N * MAX_N];

inline bool check(int i, int j)
{
    if(abs(boy[i].height - girl[j].height) > 40) return true;
    if(boy[i].music != girl[j].music) return true;
    if(boy[i].game == girl[j].game) return true;
    return false;
}

inline void AddEdge(int from, int to)
{
    edge[total].to = to;
    edge[total].next = head[from];
    head[from] = total++;
}

inline bool dfs(int u)
{
    for(int i = head[u]; i != -1; i = edge[i].next){
        int to = edge[i].to;
        if(vis[to]) continue;
        vis[to] = 1;
        if(match[to] == -1 || dfs(match[to])){
            match[to] = u;
            return true;
        }
    }
    return false;
}

inline int Hungary()
{
    int res = 0;
    memset(match, -1, sizeof(match));
    for(int i = 0; i < n; i++){
        memset(vis, 0, sizeof(vis));
        if(dfs(i)) res++;
    }
    return res;
}

int main()
{
    IOS;
    cin >> T;
    while(T--){
        cin >> K;
        n = m = 0;
        for(int i = 0; i < K; i++){
            int h;
            string tmp1, tmp2, tmp3;
            cin >> h >> tmp1 >> tmp2 >> tmp3;
            if(tmp1[0] == 'M') {
                boy[n].height = h, boy[n].music = tmp2;
                boy[n].game = tmp3, n++;
            }else {
                girl[m].height = h, girl[m].music = tmp2;
                girl[m].game = tmp3, m++;
            }
        }
        total = 0;
        memset(head, -1, sizeof(head));
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                if(check(i, j) == false ) AddEdge(i, j);
            }
        }
        int ans = Hungary();
        cout << K - ans << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值