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四个标准都不符合 ...
随机推荐
- baguetteBox.js响应式画廊插件(纯JS)
baguetteBox.js baguetteBox.js 是一个简单和易于使用lightbox纯JavaScript脚本,拥有图像放大缩小并带有相应的CSS3过度,并能在触摸屏等设备上完美展示. D ...
- mobilebone.js使用笔记
mobilebone.js主要用来是网页呈现单页效果,添加类似native app的页面切换效果.原理是:当打开a链接里的页面时,不再以传统的新页面打开,而是以ajax-html的方式,将新页面的内容 ...
- JavaScript 语句 数组与冒泡排序法
数组 数组:不管是什么类型,都可以进行存放.存放是有一定顺序的. 顺序:索引号从0开始. 1.需要先对数组进行初始化 var array //数组名称 = new Arrary() //Array 注 ...
- ooj 1066 青蛙过河DP
http://121.249.217.157/JudgeOnline/problem.php?id=1066 1066: 青蛙过河 时间限制: 1 Sec 内存限制: 64 MB提交: 58 解决 ...
- javascript基础08
发现今天居然没有要写,那我就写写之前做的笔记吧. 这是事件的深入: 拖拽逻辑: 第一个: onmousedown : 选择元素 第二个: onmousemove : 移动元素 第三个:onmouseu ...
- UHF桌面式发卡器
UHF桌面式发卡器: http://www.rr-rfid.com/index.php/pro_view/95 本文用菊子曰发布
- [原创]推荐一款强大的.NET程序内存分析工具.NET Memory Profiler
[原创]推荐一款强大的.NET程序内存分析工具.NET Memory Profiler 1 官方网站:http://memprofiler.com/2 下载地址:http://memprofiler. ...
- inotify-tools使用方法介绍
原文 inotify-tools 是为linux下inotify文件监控工具提供的一套c的开发接口库函数,同时还提供了一系列的命令行工具,这些工具可以用来监控文件系统的事件. inotify-tool ...
- 用SDWebImage渐变加载图片
用SDWebImage渐变加载图片 使用 使用请详细查看源码,只需要给定一个图片地址以及一个placeHolder图片(非必须)即可. 效果 源码 https://github.com/YouXian ...
- redis 学习指南
一.介绍 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.一个高性能的key-value数据库.并提供多种语言的API.说到Key-Value数据库NoSQL数 ...