题目链接:http://poj.org/problem?id=2771

Guardian of Decency
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 5517   Accepted: 2322

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

Source

 
看到题意我笑了。
题意:人和人之间如果满足4个条件的任意一条就不会成为夫妻,给定一些人,问从中最多能选多少人且保证任意两人不可能成为夫妻。
分析:由于条件之一是性别相同则不能成为夫妻。我们根据性别把人划分为两个集合,每个人是一个结点构成二分图,若两个人可能成为夫妻则连一条边。然后最大独立集 = 顶点数 - 最大匹配数。
#include <stdio.h>
#include <string.h>
#include <math.h> bool maps[][];
bool use[];
int match[]; struct Stu {
int h;
char sex[];
char music[];
char sport[];
}mans[],womans[]; int nman,nwoman; bool judge(Stu a,Stu b)
{
if(strcmp(a.music,b.music)) return false;
if(!strcmp(a.sport,b.sport)) return false;
if(fabs((a.h-b.h)*1.0)>) return false;
return true;
} bool DFS(int u)
{
for(int i=;i<nwoman;i++)
{
if(!use[i]&&maps[u][i])
{
use[i] = true;
if(match[i]==-||DFS(match[i]))
{
match[i] = u;
return true;
}
}
}
return false;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(maps,false,sizeof(maps));
memset(match,-,sizeof(match));
int num;
nman = ;
nwoman = ;
scanf("%d",&num);
for(int i=;i<num;i++)
{
int height;
char sex[];
char musics[];
char sports[];
scanf("%d%s%s%s",&height,sex,musics,sports);
if(sex[]=='M')
{
mans[nman].h = height;
strcpy(mans[nman].sex,sex);
strcpy(mans[nman].music,musics);
strcpy(mans[nman++].sport,sports);
}
if(sex[]=='F')
{
womans[nwoman].h = height;
strcpy(womans[nwoman].sex,sex);
strcpy(womans[nwoman].music,musics);
strcpy(womans[nwoman++].sport,sports);
}
} for(int i=;i<nman;i++)
{
for(int j=;j<nwoman;j++)
{
if(judge(mans[i],womans[j]))
maps[i][j] = true;
}
} int ans = ;
for(int i=;i<nman;i++)
{
memset(use,false,sizeof(use));
if(DFS(i))
ans++;
}
printf("%d\n",num-ans); }
return ;
}

Poj(2771),最大独立集的更多相关文章

  1. poj 2771 最大独立集

    这道题又无耻的抄袭了别人的代码. 刚开始以为是最大匹配,把条件不相符的人连一起,然后求最大匹配,感觉麻烦,然后看了别人的解题报告,是把相符的人连一起,然后减去,其实就是最大独立集. 最大独立集=|G| ...

  2. POJ 2771 最大独立集 匈牙利算法

    (为什么最大独立集的背景都是严打搞对象的( _ _)ノ|壁) 思路:匈牙利算法 没什么可说的-- // by SiriusRen #include <cstdio> #include &l ...

  3. poj——2771 Guardian of Decency

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

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

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

  5. POJ 2771 Guardian of Decency(最大独立集数=顶点数-最大匹配数)

    题目链接: http://poj.org/problem?id=2771 Description Frank N. Stein is a very conservative high-school t ...

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

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

  7. Guardian of Decency POJ - 2771 【二分匹配,最大独立集】

    Problem DescriptionFrank N. Stein is a very conservative high-school teacher. He wants to take some ...

  8. poj 2771 Guardian of Decency 解题报告

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

  9. POJ 2771 Guardian of Decency

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

随机推荐

  1. 网易免费邮件开启smtp教程

    网易免费邮件开启smtp教程     作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们在部署zabbix邮件报警中可能会用到163.com.那么我们如何去开启smtp服务呢? 1 ...

  2. Codeforce Round #228 Div2

    这次的A题没注意要到100- -, B题没做,后来做要注意下1和long long C题当时坑的一B,用了个蠢办法,后来还错了,现在改了,还是蠢办法,等等再去用dp吧,而且本来就只有01用个鸡巴的树状 ...

  3. C++之路进阶——边表

    边表:利用边的关系来表示一个图. 用到数组: head//head[i]表示从i点出发的第一条边的编号; next[i]//与第i条边起点相同的下一条边的编号; a[i]//第i条边的终点; val[ ...

  4. Sping中的事务配置

    关于Spring的事务配置,主要的配置文件如下(使用了C3P0连接池): <?xml version="1.0" encoding="UTF-8"?> ...

  5. Java的正则表达式

    package RegexTest; /** * Created by hu on 2016/3/29. */ /* * Java的正则表达式 在正则表达式中,用\d表示一位数字,如果在其它语言中使用 ...

  6. linux第13天 生产者与消费者

    pthread_cond_t   my_condition = PTHREAD_COND_INITIALIZER; pthread_mutex_t mutex = PTHREAD_MUTEX_INIT ...

  7. Spring之我见

    Spring 是什么(1) •Spring 是一个开源框架. •Spring 为简化企业级应用开发而生. 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能. • ...

  8. Linode 优惠码

    Linode 是最好的vps $10的优惠码 Linode10,推荐码:bc7852453e280eee5a8ef045c5ab54ca091c8021 链接https://www.linode.co ...

  9. 获取CPUID等

    unit CommonUnit; interface uses Windows, SysUtils, DateUtils; Const CPUVendorIDs: .. ] of string = ( ...

  10. 作为WEB工程师,我们是不是应该积极的推进一下用户浏览器的使用体验?

    为什么会写这篇文章,其实是有原因的.目前我工作的公司的Web网站仅支持IE8以上的版本,然后我们经常接到客户的反馈,说为什么在他浏览器当中flash怎么显示不了,或者为什么在他浏览器中有这样那样的问题 ...