Guardian of Decency POJ - 2771 【二分匹配,最大独立集】
Problem 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
题意:老师带学生去旅游,但是担心学生会发生恋爱关系,因此带出去的学生彼此间要满足以下条件中的一个:(1.身高相差 40cm 以上、2.同性、3.喜欢音乐风格不同、4.喜欢运动相同),求最多能带多少学生出去
思路:通过公式:二分图最大独立集=顶点数-二分图最大匹配。先求相爱的最大匹配数。
二分图最大独立集=顶点数-二分图最大匹配
独立集:图中任意两个顶点都不相连的顶点集合。
AC代码:
#include<algorithm>
#include<iostream>
#include<vector>
#include<stdio.h>
#include<string.h> using namespace std;
#define maxn 888
int n,m;
vector<int> v[maxn];
int vis[maxn];
int match[maxn];
struct str{
int h;
string shengfen;
string mus;
string sport;
}st[maxn];
bool ok(str a,str b){
if(abs(a.h-b.h)<=&&a.shengfen!=b.shengfen&&a.mus==b.mus&&a.sport!=b.sport)
return ;
else
return ;
}
int dfs(int u){
for(int i=;i<v[u].size();i++){
int temp=v[u][i];
if(vis[temp]==){
vis[temp]=;
if(match[temp]==||dfs(match[temp])){
match[temp]=u;
return ;
}
}
}
return ;
}
int main(){
int _;cin>>_;
while(_--){
scanf("%d",&n);
for(int i=;i<=n;i++)
v[i].clear();
for(int i=;i<=n;i++){
// cin>>st[i].h
scanf("%d",&st[i].h);
cin>>st[i].shengfen>>st[i].mus>>st[i].sport;
//scanf("%s%s%s",st[i].shengfen,st[i].mus,st[i].sport);
}
for(int i=;i<=n;i++){
for(int j=+i;j<=n;j++){
if(ok(st[i],st[j])){
v[i].push_back(j);
v[j].push_back(i);
}
}
}
for(int i=;i<=n;i++)
match[i]=;
// memset(match,0,sizeof(match));
int ans=;
for(int i=;i<=n;i++){
//memset(vis,0,sizeof(vis));
for(int i=;i<=n;i++)
vis[i]=;
if(dfs(i))
ans++;
}
//cout<<ans<<endl;
int res=n-ans/;
printf("%d\n",res); }
return ;
}
Guardian of Decency POJ - 2771 【二分匹配,最大独立集】的更多相关文章
- POJ 2771 二分图(最大独立集)
Guardian of Decency Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5244 Accepted: 21 ...
- HDU - 1068 Girls and Boys(二分匹配---最大独立集)
题意:给出每个学生的标号及与其有缘分成为情侣的人的标号,求一个最大集合,集合中任意两个人都没有缘分成为情侣. 分析: 1.若两人有缘分,则可以连一条边,本题是求一个最大集合,集合中任意两点都不相连,即 ...
- poj 2446 (二分匹配)
题意:除了所给的一些点外,问能不能用1*2的矩形覆盖所有的点,矩形间不能重叠. 思路:简单二分匹配,,,,,,, #include<stdio.h> #include<string. ...
- hdu 4169 二分匹配最大独立集 ***
题意:有水平N张牌,竖直M张牌,同一方向的牌不会相交.水平的和垂直的可能会相交,求最少踢出去几张牌使剩下的牌都不相交. 二分匹配 最小点覆盖=最大匹配. 链接:点我 坐标点作为匹配的端点 #inclu ...
- hdu3829 二分匹配 最大独立集
Cat VS Dog Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others) Problem ...
- Light OJ 1373 Strongly Connected Chemicals 二分匹配最大独立集
m种阳离子 n种阴离子 然后一个m*n的矩阵 第i行第j列为1代表第i种阴离子和第j种阴离子相互吸引 0表示排斥 求在阳离子和阴离子都至少有一种的情况下 最多存在多少种离子能够共存 阴阳离子都至少须要 ...
- POJ 1498[二分匹配——最小顶点覆盖]
题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=1498] 题意:给出一个大小为n*n(0<n<100)的矩阵,矩阵中放入m种颜色(标号为1 ...
- POJ 2771 Guardian of Decency 【最大独立集】
传送门:http://poj.org/problem?id=2771 Guardian of Decency Time Limit: 3000MS Memory Limit: 65536K Tot ...
- poj——2771 Guardian of Decency
poj——2771 Guardian of Decency Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5916 ...
随机推荐
- windows MinGW gcc 编译乱码问题
问题描述 一般很多编辑器默认都是保存成utf-8文件,然而在输出中文的时候出现了乱码?另外试了其他方法,有的乱码,有的不乱? MinGW gcc 编译 utf-8 文件的时候乱码 MinGW gcc ...
- Django查找数据库objects.filter() 排序order_by Q()与或非 F()属性之间比较 聚合函数的用法
条件选取QuerySet的时候,filter表示=参数可以写查询条件,exclude表示!=,querySet.distinct() 去重复(除了get返回值都是QuerySet,可以继续调用所有函数 ...
- 网络模式: host-only & NAT & 桥接
基本上,Host-only相当于虚拟机和宿主机通过交叉线相连:NAT,宿主机相当于虚拟机的路由器:桥接,相当于把宿主机和虚拟机同时接到交换机上,然后交换机接到外网. 连接性上说,可参考下表: 连接 宿 ...
- 前端开发 Vue -3axios
Axios是什么? 应该念“阿克希奥斯”……但是太长太拗口,我一般念“阿笑斯”…… Axios 是一个基于 promise 的 HTTP 库,简单的讲就是可以发送get.post请求.说到get.po ...
- [学习笔记]pb_ds库
前言 其实我很早开始就用pb_ds库了,用起来确实方便.但最近感觉还是对这个了解颇少,还是来补一下 话说有人会忘记头文件,其实这有个伎俩,找到电脑上的g++文件夹.Ubuntu应该在etc中,Wind ...
- 一次腾讯云centos服务器被入侵的处理
昨天一大早,我还没到公司呢,就收到腾讯云安全中心发来的服务器异常登录告警,登录控制台一看,ip还是美国的,一脸懵逼.由于本人之前也没有过处理服务器入侵的经验,而且这台服务器目前还没有部署商用系统,所以 ...
- pickle 和 base64 模块的使用
pickle pickle模块是python的标准模块,提供了对于python数据的序列化操作,可以将数据转换为bytes类型,其序列化速度比json模块要高. pickle.dumps() 将pyt ...
- 【Salesforce】入门篇
Salesforce.com 一开始是一个云端的销售自动化(Sales Force Automation, SFA)以及客户关系管理工具(Customer Relationship Managemen ...
- 什么是DDOS
什么是DDOS?分布式拒绝服务攻击(Distributed Denial of Service).百度的解释有一个形象的例子我认为比较好理解,照搬如下: 一群恶霸试图让对面那家有着竞争关系的商铺无 ...
- json _ ajax_跨域
1 json 1 js 对象 语法: 1 通过一对{}表示一个对象 2 在{}中允许通过 key:value 的形式来表示属性 3 多对的属性和值之间使用 , 隔开 2 什么中JSON 按照JS对象的 ...