Guardian of Decency UVA - 12083

6 篇文章 0 订阅

题目传送门

题意:Frank是一个思想有些保守的老师。有一次,他需要带一些学生出去旅游,但又怕其中有一些学生在旅途中萌生爱意。为了降低这种事情发生的概率,他决定确保带出去的任意两个学生至少要满足下面4条中的一条:
(1)身高相差大于40厘米。
(2)性别相同。
(3)最喜欢的音乐属于不同的类型。
(4)最喜欢的体育比赛相同。
最多能带出去多少人

思路:比赛的时候没做出来,想到这个是一个二分图匹配了,但是没有想出来怎么建图然久崩了。赛后仔细想了一下,如果是正向建图的话,将男女分开的话,用男生去匹配女生,选出来的女生还是无法保证能够与所有的男生都合适。那么就去逆向建图,把所有的男生去和不适合女生进行匹配,这个样子最后选出来所有不能在一起的,剩余的所有人就是可以在一起的。

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>

#define MAXN 510
#define MAXE 210
#define INF 10000000
#define MOD 1000000007
#define LL long long
#define pi acos(-1.0)

using namespace std;

struct Node {
  int h;
  string sex;
  string music;
  string sport;
} p[MAXN];
int n;
int match[MAXN];
bool check[MAXN];
vector<int> vec[MAXN];
set<int> Set;

bool dfs(int x, int y) {
  for (int i = 0; i < vec[x].size(); ++i) {
    if (!check[vec[x][i]]) {
      check[vec[x][i]] = true;
      if (match[vec[x][i]] == -1 || dfs(match[vec[x][i]], x)) {
        match[vec[x][i]] = x;
        return true;
      }
    }
  }
  return false;
}

int main() {
  std::ios::sync_with_stdio(false);
  int T;
  cin >> T;
  for (int kase = 1; kase <= T; ++kase) {
    Set.clear();
    memset(vec, 0, sizeof(vec));
    memset(match, -1, sizeof(match));
    cin >> n;
    for (int i = 1; i <= n; ++i) {
      cin >> p[i].h >> p[i].sex >> p[i].music >> p[i].sport;
    }
    int cnt = 0;
    for (int i = 1; i <= n; ++i) {
      if (p[i].sex == "F") {
        continue;
      }
      cnt++;
      for (int j = 1; j <= n; ++j) {
        if (p[j].sex == "F" &&
            !(labs(p[i].h - p[j].h) > 40 || p[i].music != p[j].music ||
              p[i].sport == p[j].sport)) {
          vec[i].push_back(j);
        }
      }
    }
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
      if (p[i].sex == "F") {
        continue;
      }
      memset(check, false, sizeof(check));
      if (dfs(i, -1)) {
        ans++;
      }
    }
    cout << n - ans << endl;
  }
  return 0;
}

/*
2
4
35 M classicism programming
0 M baroque skiing
43 M baroque chess
30 F baroque soccer
8
27 M romance programming
194 F baroque programming
67 M baroque ping-pong
51 M classicism programming
80 M classicism Paintball
35 M baroque ping-pong
39 F romance ping-pong
110 M romance Paintball
*/
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值