POJ 2771 Guardian of Decency(最大独立集数=顶点数-最大匹配数)
题目链接:
http://poj.org/problem?id=2771
Description
- 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
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
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
/*
问题
输入n个人的信息,如果他们满足几个条件就视为存在成为一对的关系,求不能成为一对的最大子独立集的顶点个数 解题思路
图论的题很绕,让我们理清关系,求最大子图,就是挑几个人,他们相互之间都存在一种不能成为一对的关系,最多有多少人
做图的时候,将存在能成为一对的关系的置为1,否则都置为0,跑一边匈牙利算法得到最大匹配数
那么顶点数n减去最大匹配数就是最大子独立集的个数了。
*/
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<cmath> using namespace std;
const int maxn=;
int n,e[maxn][maxn],fn,mn;
int cx[maxn],cy[maxn];
int book[maxn];
struct PER{
int age;
string sex,music,sport;
}fper[maxn],mper[maxn]; int ok(struct PER a, struct PER b);
int maxmatch();
int dfs(int u); int main()
{
int T,i,j;
int x;
char s1[],s2[],s3[];
scanf("%d",&T);
while(T--){
scanf("%d",&n);
fn=mn=;
for(i=;i<n;i++){
scanf("%d%s%s%s",&x,s1,s2,s3);
if(strcmp(s1,"F") == ){
fper[fn].age=x;
fper[fn].sex=s1;
fper[fn].music=s2;
fper[fn].sport=s3;
fn++;
}
else{
mper[mn].age=x;
mper[mn].sex=s1;
mper[mn].music=s2;
mper[mn].sport=s3;
mn++;
}
} memset(e,,sizeof(int)*maxn*maxn);
for(i=;i<mn;i++){
for(j=;j<fn;j++){
if(ok(mper[i],fper[j]))
e[i][j]=;
}
}
printf("%d\n",n-maxmatch());
}
return ;
} int ok(struct PER a, struct PER b){
if(abs(a.age - b.age) > )
return ;
if(a.sex == b.sex)
return ;
if(a.music != b.music)
return ;
if(a.sport == b.sport)
return ; return ;
} int dfs(int u)
{
for(int i=;i<fn;i++){
if(book[i] == && e[u][i]){
book[i]=; if(cy[i] == - || dfs(cy[i])){
cx[u]=i;
cy[i]=u;
return ;
}
}
}
return ;
} int maxmatch()
{
int i;
memset(cx,-,sizeof(cx));
memset(cy,-,sizeof(cy));
int ans=;
for(i=;i<mn;i++){
if(cx[i] == -){
memset(book,,sizeof(book));
if(dfs(i)) ans++;
}
}
return ans;
}
POJ 2771 Guardian of Decency(最大独立集数=顶点数-最大匹配数)的更多相关文章
- poj——2771 Guardian of Decency
poj——2771 Guardian of Decency Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5916 ...
- 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 解题报告
题目链接:http://poj.org/problem?id=2771 题目意思:有一个保守的老师要带他的学生来一次短途旅行,但是他又害怕有些人会变成情侣关系,于是就想出了一个方法: 1.身高差距 ...
- POJ 2771 Guardian of Decency (二分图最大点独立集)
Guardian of Decency Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6133 Accepted: 25 ...
- UVA 12083 POJ 2771 Guardian of Decency
/* http://acm.hust.edu.cn/vjudge/contest/view.action?cid=71805#problem/C */ 性质: [1]二分图最大点独立数=顶点数-二分图 ...
- poj 2771 Guardian of Decency(最大独立数)
题意:人与人之间满足4个条件之一即不能成为一对(也就说这4个条件都不满足才能成为一对),求可能的最多的单身人数. 思路:把男女分为两部分,接下来就是二分图的匹配问题.把能成为一对的之间连边,然后求出最 ...
- POJ 2771 Guardian of Decency
http://poj.org/problem?id=2771 题意: 一个老师想带几个同学出去,但是他怕他们会谈恋爱,所以带出去的同学两两之间必须满足如下条件之一: ①身高差大于40 ②同性 ③喜欢 ...
- POJ 2771 Guardian of Decency(求最大点独立集)
该题反过来想:将所有可能发生恋爱关系的男女配对,那么可以带出去的人数应该等于这个二分图的最大独立集 先要做一下预处理,把不符合要求的双方先求出来, company[i][j]表示i.j四个标准都不符合 ...
- UVALive3415 Guardian of Decency —— 最大独立集
题目链接:https://vjudge.net/problem/UVALive-3415 题解: 题意:选出尽可能多的人, 使得他(她)们之间不会擦出火花.即求出最大独立集. 1.因为性别有男女之分, ...
随机推荐
- 微赞微擎V0.8以上版本:【数据库读写分离】实战教程 [复制链接]
http://www.efwww.com/forum.php?mod=viewthread&tid=4870 马上注册,下载更多源码,让你轻松玩转微信公众平台. 您需要 登录 才可以下载或查看 ...
- Button去除边框方法
<Button Content="Button" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey} ...
- 新建WebAPI项目时遇到的问题
1 处理程序“ExtensionlessUrlHandler-Integrated-4.0”在其模块列表中有一个错误模块“ManagedPipelineHandler” 以管理员运行下面的命令注册 ...
- Android---------------Activity的学习
一.Activity的启动方式 1.显示启动 Intent intent=new Intent(MainActivity.this,SettingActivity.class); //还可以这 ...
- jzoj3086 [分層圖最短路]
分層圖最短路即可 #include<bits/stdc++.h> using namespace std; #define N 1000010 int n,m,v[N*2],nxt[N*2 ...
- Android自定义组合控件详细示例 (附完整源码)
在我们平时的Android开发中,有时候原生的控件无法满足我们的需求,或者经常用到几个控件组合在一起来使用.这个时候,我们就可以根据自己的需求创建自定义的控件了,一般通过继承View或其子类来实现. ...
- 在redis中使用lua脚本
在实际工作过程中,可以使用lua脚本来解决一些需要保证原子性的问题,而且lua脚本可以缓存在redis服务器上,势必会增加性能. 不过lua也会有很多限制,在使用的时候要注意. 在Redis中执行Lu ...
- [Swift]lower_bound和upper_bound
时间复杂度:一次查询O(log n),n为数组长度. [C++] lower_bound:返回大于等于val的第一个值 功能:查找非递减序列[first,last) 内第一个大于或等于某个元素的位置. ...
- 修改git全局初始化的用户名
今天我把文件上传到码云的时候出现 翻译的话就是 用户名不能多个值 这样的错误 所以想要解决这个错误的话非常简单 $ git config --global --replace-all user ...
- cookie和session的区别,分布式环境怎么保存用户状态
cookie和session的区别,分布式环境怎么保存用户状态 1.cookie数据存放在客户的浏览器上,session数据放在服务器上. 2.cookie不是很安全,别人可以分析存放在本地的COOK ...