uva 12083 Guardian of Decency

Description

Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is afraid that some of them might become couples. While you can never exclude this possibility, he has made some rules that he thinks indicates a low probability two persons will become a couple:

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).

So, for any two persons that he brings on the excursion, they must satisfy at least one of the requirements above. Help him find the maximum number of persons he can take, given their vital information.

Input

The first line of the input consists of an integer T ≤ 100 giving the number of test cases. The first line of each test case consists of an integer N ≤ 500 giving the number of pupils. Next there will be one line for each pupil consisting of four space-separated data items:

an integer h giving the height in cm;
a character 'F' for female or 'M' for male;
a string describing the preferred music style;
a string with the name of the favourite sport.

No string in the input will contain more than 100 characters, nor will any string contain any whitespace.

Output

For each test case in the input there should be one line with an integer giving the maximum number of eligible pupils.

Sample Input

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

Sample Output

3

7

题目大意:Frank是一个思想有些保守的老师。有一次,他须要带一些学生出去旅游,但又怕当中有一些学生在旅途中萌生爱意。

为了减少这样的事情发生的概率,他决定确保带出去的随意两个学生至少要满足以下4条中的一条:

1)身高相差大于40厘米。

2)性别同样。

3)最喜欢的音乐属于不同的类型。

4)最喜欢的体育比赛同样。

你的任务是帮助Frank挑选尽量多的学生,使得随意两个学生至少满足上述条件中的一条。

解题思路:将能够匹配的男女建边。然后就是一个二分图匹配问题。

最后用n减去求出的最大的匹配。就是答案。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std; typedef long long ll;
const int N = 505;
const int M = 250005;
struct Node {
int height, id;
char sex[5], music[105], sport[105];
}stu[N];
int n, e, ans;
int head[N], pnt[M], Next[M], rec[N];
bool S[N], T[N]; void init() {
e = 0;
memset(head, -1, sizeof(head));
memset(rec, 0, sizeof(rec));
ans = 0;
} bool check(int i,int j) {
if(abs(stu[i].height - stu[j].height) > 40) return false;
//身高差超过40cm
if (strcmp(stu[i].music, stu[j].music)) return false;
//喜欢的音乐类型同样
if (!strcmp(stu[i].sport, stu[j].sport)) return false;
//喜欢的体育类型不同
return true;
} void addEdge(int u,int v) {
pnt[e] = v;
Next[e] = head[u];
head[u] = e++;
} void input() {
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d %s %s %s", &stu[i].height, stu[i].sex, stu[i].music, stu[i].sport);
}
for(int i = 1; i <= n; i++) {
if(stu[i].sex[0] == 'F') continue; //边仅仅能从男生到女生
for(int j = 1; j <= n; j++) {
//将能够匹配的男女建边
if(stu[j].sex[0] == 'F' && check(i,j)) addEdge(i,j);
}
}
} bool match(int u) {
S[u] = 1;
for(int i = head[u]; i != -1 ; i = Next[i])
if(!T[pnt[i]]) {
T[pnt[i]] = 1;
if(!rec[pnt[i]] || match(rec[pnt[i]])) {
rec[pnt[i]] = u;
return true;
}
}
return false;
} void hungary() {
for(int i = 1; i <= n; i++) {
memset(S, 0, sizeof(S));
memset(T, 0, sizeof(T));
if(match(i)) ans++;
}
} int main() {
int T;
scanf("%d", &T);
while(T--) {
init();
input();
hungary();
printf("%d\n", n - ans);
}
return 0;
}

uva 12083 Guardian of Decency (二分图匹配)的更多相关文章

  1. UVA - 12083 Guardian of Decency (二分匹配)

    题意:有N个人,已知身高.性别.音乐.运动.要求选出尽可能多的人,使这些人两两之间至少满足下列四个条件之一. 1.身高差>40  2.性别相同  3.音乐不同  4.运动相同 分析: 1.很显然 ...

  2. UVA 1663 Purifying Machine (二分图匹配,最大流)

    题意: 给m个长度为n的模板串,模板串由0和1和*三种组成,且每串至多1个*,代表可0可1.模板串至多匹配2个串,即*号改成0和1,如果没有*号则只能匹配自己.问:模板串可以缩减为几个,同样可以匹配原 ...

  3. UVA-12083 Guardian of Decency 二分图 最大独立集

    题目链接:https://cn.vjudge.net/problem/UVA-12083 题意 学校组织去郊游,选择最多人数,使得任意两个人之间不能谈恋爱 不恋爱条件是高差大于40.同性.喜欢的音乐风 ...

  4. UVa 二分图匹配 Examples

    这些都是刘汝佳的算法训练指南上的例题,基本包括了常见的几种二分图匹配的算法. 二分图是这样一个图,顶点分成两个不相交的集合X , Y中,其中同一个集合中没有边,所有的边关联在两个集合中. 给定一个二分 ...

  5. POJ 2771 Guardian of Decency (二分图最大点独立集)

    Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6133   Accepted: 25 ...

  6. Guardian of Decency(二分图)

    Guardian of Decency Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submi ...

  7. POJ 2289 Jamie's Contact Groups / UVA 1345 Jamie's Contact Groups / ZOJ 2399 Jamie's Contact Groups / HDU 1699 Jamie's Contact Groups / SCU 1996 Jamie's Contact Groups (二分,二分图匹配)

    POJ 2289 Jamie's Contact Groups / UVA 1345 Jamie's Contact Groups / ZOJ 2399 Jamie's Contact Groups ...

  8. UVA 12549 - 二分图匹配

    题意:给定一个Y行X列的网格,网格种有重要位置和障碍物.要求用最少的机器人看守所有重要的位置,每个机器人放在一个格子里,面朝上下左右四个方向之一发出激光直到射到障碍物为止,沿途都是看守范围.机器人不会 ...

  9. UVALive 3415 Guardian of Decency(二分图的最大独立集)

    题意:老师在选择一些学生做活动时,为避免学生发生暧昧关系,就提出了四个要求.在他眼中,只要任意两个人符合这四个要求之一,就不可能发生暧昧.现在给出n个学生关于这四个要求的信息,求老师可以挑选出的最大学 ...

随机推荐

  1. 打造好用的编辑终端环境yakuake

    我喜欢使用vim,由于不喜欢不够纯粹的东西,所以将伪终端打造的和真终端几乎一样,我的yakuake全屏是真的全屏,你也想这样,跟我来吧. 一.修改yakuake的皮肤文件,在/usr/share/ya ...

  2. uva 177:Paper Folding(模拟 Grade D)

    题目链接 题意:一张纸,每次从右往左对折.折好以后打开,让每个折痕都自然的呈90度.输出形状. 思路:模拟折……每次折想象成把一张纸分成了正面在下的一张和反面在上的一张.维护左边和方向,然后输出.细节 ...

  3. C# 键值对的类型

    一 C# 键值对类有以下类: ①    IDictionary<string, Object> idc = new Dictionary<string, object>(); ...

  4. AC日记——营业额统计 codevs 1296 (splay版)

    营业额统计 思路: 每次,插入一个点: 然后找前驱后继: 来,上代码: #include <cmath> #include <cstdio> #include <iost ...

  5. 记一个react拖动排序中的坑:key

    在做一个基于react的应用的时候遇到了对列表拖动排序的需求.当使用sortable对列表添加排序支持后发现一个问题:数据正确排序了,但是dom的顺序却乱了,找了一会儿原因后发现是因为在渲染数据的时候 ...

  6. weblogic内存快速配置

    # IF USER_MEM_ARGS the environment variable is set, use it to override ALL MEM_ARGS values USER_MEM_ ...

  7. python第三方库离线安装-使用pip

    参考:http://www.cnblogs.com/michael-xiang/p/5690746.html 操作系统:CentOS 6.9 python:2.7.14 (默认的2.6.6需要升级到2 ...

  8. Algorithm | Vector

    因为平常用的话只是vector的一些非常简单的功能,基本上把它当数组来用,现在也只是把这一部分写了一些. template<class T> class XVector { public: ...

  9. 聊聊、Zookeeper Linux 单服务

    关于上一篇 Zookeeper 的文章是介绍安装启动,这一篇介绍独立服务,也就是单台 Zookeeper 提供服务.首先登陆 Linux 系统,确保网络通畅.如果遇到找不到网卡 eth0 情况,可以先 ...

  10. PHP计算两个时间的年数、月数以及天数

    如何获取两个不同时间相差几年几月几日呢?比如当前时间距离2008年08月08日的北京奥运会有几年几月几日了?需要说明的是:1.定义一年为360天,一个月为30天:2.代码中86400=24*60*60 ...