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 【二分匹配,最大独立集】的更多相关文章

  1. POJ 2771 二分图(最大独立集)

    Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5244   Accepted: 21 ...

  2. HDU - 1068 Girls and Boys(二分匹配---最大独立集)

    题意:给出每个学生的标号及与其有缘分成为情侣的人的标号,求一个最大集合,集合中任意两个人都没有缘分成为情侣. 分析: 1.若两人有缘分,则可以连一条边,本题是求一个最大集合,集合中任意两点都不相连,即 ...

  3. poj 2446 (二分匹配)

    题意:除了所给的一些点外,问能不能用1*2的矩形覆盖所有的点,矩形间不能重叠. 思路:简单二分匹配,,,,,,, #include<stdio.h> #include<string. ...

  4. hdu 4169 二分匹配最大独立集 ***

    题意:有水平N张牌,竖直M张牌,同一方向的牌不会相交.水平的和垂直的可能会相交,求最少踢出去几张牌使剩下的牌都不相交. 二分匹配 最小点覆盖=最大匹配. 链接:点我 坐标点作为匹配的端点 #inclu ...

  5. hdu3829 二分匹配 最大独立集

    Cat VS Dog Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others) Problem ...

  6. Light OJ 1373 Strongly Connected Chemicals 二分匹配最大独立集

    m种阳离子 n种阴离子 然后一个m*n的矩阵 第i行第j列为1代表第i种阴离子和第j种阴离子相互吸引 0表示排斥 求在阳离子和阴离子都至少有一种的情况下 最多存在多少种离子能够共存 阴阳离子都至少须要 ...

  7. POJ 1498[二分匹配——最小顶点覆盖]

    题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=1498] 题意:给出一个大小为n*n(0<n<100)的矩阵,矩阵中放入m种颜色(标号为1 ...

  8. POJ 2771 Guardian of Decency 【最大独立集】

    传送门:http://poj.org/problem?id=2771 Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K Tot ...

  9. poj——2771 Guardian of Decency

    poj——2771    Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5916   ...

随机推荐

  1. php文件操作类

    <?php /** *本类为文件操作类,实现了文件的建立,写入,删除,修改,复制,移动,创建目录,删除目录 * 列出目录里的文件等功能,路径后面别忘了加"/" */ clas ...

  2. IDEA中通过Maven插件使用MyBatis Generator

    这样做更简单,参考: IDEA集成MyBatis Generator 插件 详解

  3. B-JUI文档、下载

    概述 特别说明 本文档仅适用于最新版的B-JUI,网站首页或下载页可以查看B-JUI的最后更新时间. B-JUI前端管理框架适用于快速开发各类WEB管理系统,可与任意后端程序(java.php..ne ...

  4. Java HeapSort

    Java HeapSort /** * <html> * <body> * <P> Copyright 1994-2018 JasonInternational & ...

  5. Python 生成动态变量 调用动态变量

    动态生成变量: variable = locals() for i in range(10): variable['A'+str(i)] = 123 print(A8) 调用动态变量: v = loc ...

  6. MySQL数据库的创建&删除&选择

    1.MySQL数据库的创建 方法1和2      方法3.使用PHP脚本 PHP中使用mysqli_query函数来创建或删除MySql数据库 mysqli_query函数:两个参数 返回值:执行成功 ...

  7. 3 webpack 4 加vue 2.0生产环境搭建

    1 在前两篇笔记中已经能把开发环境弄好了,接来下构建他的生产环境 2 使用npm 安装url-loader和file-loader来支持图片和字体 npm install --save-dev url ...

  8. MVC框架+vue+elementUI

    用自动化构建做的vue项目,因为是动态加载数据,在SEO优化时一直不如意,于是我们换了框架,用MVC框架,做成静态页面,但是原来的代码都是用vue和elementUI,为了快速的复用原来的代码,于是在 ...

  9. # JDK7+ MethodHandle

    简单例子 import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.i ...

  10. xposed获取类的属性成员

    XposedBridge.log("开始获取属性:"); Field[] fields = param.thisObject.getClass().getDeclaredField ...