题目链接:

http://poj.org/problem?id=2771

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
 /*
问题
输入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(最大独立集数=顶点数-最大匹配数)的更多相关文章

  1. poj——2771 Guardian of Decency

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

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

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

  3. poj 2771 Guardian of Decency 解题报告

    题目链接:http://poj.org/problem?id=2771 题目意思:有一个保守的老师要带他的学生来一次短途旅行,但是他又害怕有些人会变成情侣关系,于是就想出了一个方法: 1.身高差距   ...

  4. POJ 2771 Guardian of Decency (二分图最大点独立集)

    Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6133   Accepted: 25 ...

  5. UVA 12083 POJ 2771 Guardian of Decency

    /* http://acm.hust.edu.cn/vjudge/contest/view.action?cid=71805#problem/C */ 性质: [1]二分图最大点独立数=顶点数-二分图 ...

  6. poj 2771 Guardian of Decency(最大独立数)

    题意:人与人之间满足4个条件之一即不能成为一对(也就说这4个条件都不满足才能成为一对),求可能的最多的单身人数. 思路:把男女分为两部分,接下来就是二分图的匹配问题.把能成为一对的之间连边,然后求出最 ...

  7. POJ 2771 Guardian of Decency

    http://poj.org/problem?id=2771 题意: 一个老师想带几个同学出去,但是他怕他们会谈恋爱,所以带出去的同学两两之间必须满足如下条件之一: ①身高差大于40  ②同性 ③喜欢 ...

  8. POJ 2771 Guardian of Decency(求最大点独立集)

    该题反过来想:将所有可能发生恋爱关系的男女配对,那么可以带出去的人数应该等于这个二分图的最大独立集 先要做一下预处理,把不符合要求的双方先求出来, company[i][j]表示i.j四个标准都不符合 ...

  9. UVALive3415 Guardian of Decency —— 最大独立集

    题目链接:https://vjudge.net/problem/UVALive-3415 题解: 题意:选出尽可能多的人, 使得他(她)们之间不会擦出火花.即求出最大独立集. 1.因为性别有男女之分, ...

随机推荐

  1. SRM483

    250pt 题意:给定一个[0,1)间的实数,一个分母不超过maxDen的分数逼近.. 思路:直接枚举.然后判断. code: #line 7 "BestApproximationDiv1. ...

  2. 3.Ubuntu下安装mysql并在windows下使用Navicat来连接

    一.安装mysql(只需要三条命令) 1.第一条命令(中间需要输入root用户密码): sudo apt-get install mysql-server 2.第二条命令: sudo apt-get ...

  3. OmniThreadLibrary学习笔记

    http://blog.sina.com.cn/s/articlelist_1157240623_6_1.html 非常好的控件,仔细看

  4. 如何让cxgrid既能充满又能根据内容进行宽度调整?

    如何让cxgrid既能充满又能根据内容进行宽度调整? 原创 2009年02月25日 10:10:00 2793 1.选中cxgridview,在属性中找OptionsView--->ColumA ...

  5. Android-Java控制多线程执行顺序

    功能需求: Thread-0线程:打印 1 2 3 4 5 6 Thread-1线程:打印1 1 2 3 4 5 6 先看一个为实现(功能需求的案例) package android.java; // ...

  6. .net core 与ELK(2)安装Elasticsearch可视化工具

    elasticsearch-head是els的界面插件,地址https://github.com/mobz/elasticsearch-head 1.进入github并下载 wget https:// ...

  7. datatable绑定数据源

    DataTable dt = new DataTable(); dt.Columns.Add("clmn1", System.Type.GetType("System.S ...

  8. wpf 的依赖属性只能在loaded 事件之后才能取到

    wpf 的依赖属性只能在loaded 事件之后才能取到,在构造函数的  InitializeComponent(); 之后取不到 wpf 的依赖属性只能在loaded 事件之后才能取到,在构造函数的  ...

  9. [leetcode.com]算法题目 - Pascal's Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  10. Lerning Entity Framework 6 ------ Defining Relationships

    There are three types of relationships in database. They are: One-to-Many One-to-One Many-to-Many Th ...