Guardian of Decency(二分图)
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Description
Problem H - Guardian of Decency
Time limit: 15 seconds
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.
二分图最大匹配,使用匈牙利算法解决。
根据性别将学生分为两个不交集合。
求出最大匹配数m后,n - m即为所求。
AC Code:
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define clr(a, i) memset(a, i, sizeof(a))
#define FOR0(i, n) for(int i = 0; i < n; ++i)
#define FOR1(i, n) for(int i = 1; i <= n; ++i)
#define sf scanf
#define pf printf const int MAXN = ;
struct Node
{
int h;
char gen;
char mus[];
char spo[];
void input() {
scanf("%d %c %s %s", &h, &gen, mus, spo);
}
bool satisfy(Node &y) const{
return gen != y.gen && fabs(h - y.h) <= && !strcmp(mus, y.mus) &&
strcmp(spo, y.spo);
}
}pup[MAXN];
int n;
vector<int> female, male;
bool adj[MAXN][MAXN];
int match[MAXN];
bool vis[MAXN]; void addEdge(int i)
{
adj[i][i] = false;
for (int j = ; j < i; ++j){
adj[i][j] = adj[j][i] = pup[i].satisfy(pup[j]);
}
} bool findCrossPath(int v)
{
for(int i = ; i < n; ++i)
{
if(adj[v][i] == true && vis[i] == false)
{
vis[i] = true;
if(match[i] == - || findCrossPath(match[i]))
{
match[i] = v;
return true;
}
}
}
return false;
} int Hungary()
{
int cnt = ;
memset(match, -, sizeof(match));
for(vector<int>::iterator it = male.begin(); it != male.end(); ++it)
{
memset(vis, false, sizeof(vis));
if(match[*it] == - && findCrossPath(*it))
{
++cnt;
}
}
return cnt;
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
female.clear();
male.clear();
scanf("%d", &n);
for(int i = ; i < n; ++i)
{
pup[i].input();
if(pup[i].gen == 'F') female.push_back(i);
else male.push_back(i);
addEdge(i);
}
printf("%d\n", n - Hungary());
}
return ;
}
Guardian of Decency(二分图)的更多相关文章
- UVA-12083 Guardian of Decency 二分图 最大独立集
题目链接:https://cn.vjudge.net/problem/UVA-12083 题意 学校组织去郊游,选择最多人数,使得任意两个人之间不能谈恋爱 不恋爱条件是高差大于40.同性.喜欢的音乐风 ...
- POJ 2771 Guardian of Decency (二分图最大点独立集)
Guardian of Decency Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6133 Accepted: 25 ...
- uva 12083 Guardian of Decency (二分图匹配)
uva 12083 Guardian of Decency Description Frank N. Stein is a very conservative high-school teacher. ...
- poj——2771 Guardian of Decency
poj——2771 Guardian of Decency Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5916 ...
- POJ2771_Guardian of Decency(二分图/最大独立集=N-最大匹配)
解决报告 http://blog.csdn.net/juncoder/article/details/38159017 题目传送门 题意: 看到题目我就笑了.., 老师觉得这种两个学生不是一对: 身高 ...
- Guardian of Decency UVALive - 3415 最大独立集=结点数-最大匹配数 老师带大学生旅游
/** 题目:Guardian of Decency UVALive - 3415 最大独立集=结点数-最大匹配数 老师带大学生旅游 链接:https://vjudge.net/problem/UVA ...
- POJ 2771 Guardian of Decency 【最大独立集】
传送门:http://poj.org/problem?id=2771 Guardian of Decency Time Limit: 3000MS Memory Limit: 65536K Tot ...
- UVALive 3415 Guardian of Decency(二分图的最大独立集)
题意:老师在选择一些学生做活动时,为避免学生发生暧昧关系,就提出了四个要求.在他眼中,只要任意两个人符合这四个要求之一,就不可能发生暧昧.现在给出n个学生关于这四个要求的信息,求老师可以挑选出的最大学 ...
- POJ 2771 Guardian of Decency(求最大点独立集)
该题反过来想:将所有可能发生恋爱关系的男女配对,那么可以带出去的人数应该等于这个二分图的最大独立集 先要做一下预处理,把不符合要求的双方先求出来, company[i][j]表示i.j四个标准都不符合 ...
随机推荐
- 深入理解JavaScript 事件
本文总结自<JavaScript高级程序设计>以及自己平时的经验,针对较新浏览器以及 DOM3 级事件标准(2016年8月),对少部分内容作了更正,增加了各种例子及解析. 如无特殊说明,本 ...
- paip.汉字简化大法总结
paip.汉字简化大法总结 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/attilax ...
- 解决 "Windows 无法启动Mongo DB服务 错误:1067 进程意外终止"
在启动MongoDB服务时,有时会报上图所示的错误,解决方案为: 1. MongoDB安装目录\data\将此文件夹下的mongod.lock删除 2. 查看官方文档或按照上一篇安装文章检查是否设置d ...
- 从零开始学Bootstrap(1)
最近需要做一个简单的Web页面. 考虑到前端经验不足,为了快速产出,同时项目只是一个工具,对项目没有什么要求,所以我选择了Bootstrap这个框架作为Web框架. 写从零开始学Bootstrap的初 ...
- android: 播放视频
播放视频文件其实并不比播放音频文件复杂,主要是使用 VideoView 类来实现的.这个 类将视频的显示和控制集于一身,使得我们仅仅借助它就可以完成一个简易的视频播放器. VideoView 的用法和 ...
- Redis & Python/Django 简单用户登陆
一.Redis key相关操作: 1.del key [key..] 删除一个或多个key,如果不存在则忽略 2.keys pattern keys模式匹配,符合glob风格通配符,glob风格的通配 ...
- ios相关手册、图表等综合
Objective-C初学者速查表(来源:http://www.cocoachina.com/applenews/devnews/2013/1115/7362.html) iOS UIKit类图 (来 ...
- spring中使用mockito
1 mockito介绍和入门 官方:https://github.com/mockito/mockito 入门: 5分钟了解Mockito http://liuzhijun.iteye.com/blo ...
- VMware安装RedHat Linux虚拟机图文详解
创建Red Hat Linux虚拟机 1.打开VMware,开始创建虚拟机 点击菜单[文件]->[新建虚拟机]. 2.默认典型,单击[下一步] 3.选择安装来源 在这里,我们选择安装来源为[安装 ...
- Huxley 是一个用于Web应用 UI 测试的工具
Huxley 是一个用于Web应用 UI 测试的工具,由 Pete Hunt 和 Maykel Loomans 用 Python 开发. UI 测试比较令人头疼. UI测试不好写,而且很容易失效: ...