POJ 2771 - Guardian of Decency (二分图)

Guardian of Decency

题目链接: Guardian of Decency POJ - 2771

题意

有一个古板的老师,要带学生去郊游,为了防止学生谈恋爱,所有带的学生互相之间必须满足如下条件

  • Their height differs by more than 40 cm.
  • They are of the same sex.
  • Their preferred music style is different.
  • Their favourite sport is the same (they are likely to be fans of different teams and that would result in fighting).

问最多能带多少学生?


思路

这就是一道裸的最大独立集,因为同性之间必定是无法的组成CP的,所以,这显然是一个二分图,而在二分图中有一条性质,独立集 == 总点数减去最大匹配,所有只需建图求最大匹配即可。

那么图就有两种建法,1.拆分成男女生两个点集 2.直接混合建图,但是这个要注意,因为并在一起混合建图就会导致最后的匹配数是原来二分图的两边,最后需除以二。


代码

#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cstdio>
#include <string>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <sstream>
#include <memory>
#include <iostream>
#include <algorithm>

using namespace std;
#define rep(i,j,k) for(int i = (int)j;i <= (int)k;i ++)
#define per(i,j,k) for(int i = (int)j;i >= (int)k;i --)
#define debug(x) cerr<<#x<<" = "<<(x)<<endl
#define mmm(a,b) memset(a,b,sizeof(a))
#define pb push_back

typedef double db;
typedef long long ll;
const int MAXN = (int)5e2+7;
const int INF = (int)0x3f3f3f3f;

int N;
int used[MAXN],nxt[MAXN];
vector<int> G[MAXN];

struct Node{
    int hi;
    string sex,music,sports;
    Node(int hi = 0,string s = "",string m = "",string p = ""):hi(hi),sex(s),music(m),sports(p){}
}Peo[MAXN];

bool Find(int x) {
    rep(i,0,G[x].size()-1){
        int v = G[x][i];
        if (!used[v]){
            used[v] = 1;
            if (!nxt[v] || Find(nxt[v])){
                nxt[v] = x;
                return true;
            }
        }
    }
    return false;
}

int match(){
    int sum = 0;
    mmm(nxt,0);
    rep(i,1,N) {
        mmm(used,0);
        if (Find(i)) sum ++;
    }
    return sum;
}

bool OK(int i,int j){
    if (Peo[i].sex == Peo[j].sex)        return false;
    if (abs(Peo[i].hi - Peo[j].hi) > 40) return false;
    if (Peo[i].music != Peo[j].music)    return false;
    if (Peo[i].sports == Peo[j].sports)  return false;
    return true;
}

void init(){
    rep(i,1,N) G[i].clear();
    mmm(nxt,0);
}

int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

    int T;
    cin >> T;
    while (T --){
        init();
        cin >> N;
        rep(i,1,N) {
            cin >> Peo[i].hi >> Peo[i].sex >> Peo[i].music >> Peo[i].sports;
        }
        rep(i,1,N) {
            rep(j,1,N) {
                if (i == j) continue;
                if (OK(i,j)) G[i].pb(j);
            }
        }
        cout << N - match()/2 << endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值