Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 5513   Accepted: 2319

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

求二分图最大独立集。给字符串都做好映射,然后根据题目中条件,把可能组CP的人之间连边,跑匈牙利算法就行。

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<string>
#include<vector>
#include<map>
using namespace std;
const int mxn=;
struct stu{
int h;//高度
bool se;//性别
int m,sp;
}a[mxn];
map<string,int> mpm,mpsp;
vector<int>e[mxn];
int mcnt=,spcnt=;
int n;
//
int link[mxn],vis[mxn];
//
void clear(){
mpm.clear();
mpsp.clear();
memset(a,,sizeof a);
memset(link,,sizeof link);
for(int i=;i<=n;i++) e[i].clear();
mcnt=spcnt=;
}
bool dfs(int s){//匈牙利算法
int i,j;
for(i=;i<e[s].size();i++){
int v=e[s][i];
if(!vis[v]){
vis[v]=;
if(!link[v] || dfs(link[v])){
link[v]=s;
return ;
}
}
}
return ;
}
void calc(){
int i,j;
int ans=n;
for(i=;i<=n;i++){
if(a[i].se){
memset(vis,,sizeof vis);
if(dfs(i))ans--;
}
}
printf("%d\n",ans);
}
int main(){
int T;
scanf("%d",&T);
char sex[],music[],sport[];
while(T--){
scanf("%d",&n);
clear();
int i,j;
for(i=;i<=n;i++){
cin>>a[i].h>>sex>>music>>sport;
if(sex[]=='M') a[i].se=;
else a[i].se=;
if(!mpm.count(music)){//映射音乐
mcnt++;
mpm[music]=mcnt;
}
a[i].m=mpm[music];
if(!mpsp.count(sport)){//映射运动
spcnt++;
mpsp[sport]=spcnt;
}
a[i].sp=mpsp[sport];
// printf("test : %d %d %d\n",a[i].se,a[i].m,a[i].sp);
}
for(i=;i<n;i++)
for(j=i+;j<=n;j++){
if((abs(a[i].h-a[j].h)<=)&&(a[i].se!=a[j].se)&&(a[i].m==a[j].m)&&(a[i].sp!=a[j].sp)){//连边条件
e[i].push_back(j);
e[j].push_back(i);
}
}
calc();
}
return ;
}

POJ2771 Guardian of Decency的更多相关文章

  1. Guardian of Decency(二分图)

    Guardian of Decency Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submi ...

  2. Guardian of Decency UVALive - 3415 最大独立集=结点数-最大匹配数 老师带大学生旅游

    /** 题目:Guardian of Decency UVALive - 3415 最大独立集=结点数-最大匹配数 老师带大学生旅游 链接:https://vjudge.net/problem/UVA ...

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

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

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

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

  5. uva 12083 Guardian of Decency (二分图匹配)

    uva 12083 Guardian of Decency Description Frank N. Stein is a very conservative high-school teacher. ...

  6. poj——2771 Guardian of Decency

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

  7. UVALive 3415 Guardian of Decency(二分图的最大独立集)

    题意:老师在选择一些学生做活动时,为避免学生发生暧昧关系,就提出了四个要求.在他眼中,只要任意两个人符合这四个要求之一,就不可能发生暧昧.现在给出n个学生关于这四个要求的信息,求老师可以挑选出的最大学 ...

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

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

  9. UVAlive3415 Guardian of Decency(最大独立集)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34831 [思路] 二分图的最大独立集. 即在二分图中选取最多的点, ...

随机推荐

  1. 1072: [SCOI2007]排列perm

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3000  Solved: 1875[Submit][Status][Discuss] Descript ...

  2. BZOJ1576: [Usaco2009 Jan]安全路经Travel(最短路 并查集)

    题意 给你一张无向图,保证从1号点到每个点的最短路唯一.对于每个点求出删掉号点到它的最短路上的最后一条边(就是这条路径上与他自己相连的那条边)后1号点到它的最短路的长度 Sol emmm,考场上想了个 ...

  3. BZOJ3398: [Usaco2009 Feb]Bullcow 牡牛和牝牛(dp)

    题意     约翰要带N(1≤N≤100000)只牛去参加集会里的展示活动,这些牛可以是牡牛,也可以是牝牛.牛们要站成一排.但是牡牛是好斗的,为了避免牡牛闹出乱子,约翰决定任意两只牡牛之间至少要有K( ...

  4. LOJ#6342. 跳一跳(期望)

    题意 $n \leqslant 10^5$ Sol 随便推一推就好了吧.. $f[i] = \frac{f[i] + f[i +1] + \dots f[n]}{n - i + 1} + 1$ 移一下 ...

  5. Nginx+proxy_cache图片缓存

    搭建图片缓存机制的原理在于减少数据库的负担并加快静态资源的响应. 步骤: 1. vim /usr/local/nginx/conf/nginx.conf 2. http{     ...     .. ...

  6. mysql查询哪个表数据量最大

    use information_schema;select table_name,table_rows from tables where table_schema='cargo_new' order ...

  7. JavaScript ES6功能概述(ECMAScript 6和ES2015 +)

    JavaScript在过去几年中发生了很大的变化.这些是您今天可以开始使用的12项新功能! 该语言的新增内容称为ECMAScript 6.它也称为ES6或ES2015 +. 自1995年JavaScr ...

  8. php中foreach循环遍历二维数组

    最近在用tp3.2框架,在查询的时候用到了select(),这条语句返回的是二维数组,所以在对返回的数据做处理时,遇到了些麻烦,百度了下foreach,终于用foreach解决了数据的筛选问题 (因为 ...

  9. PHP switch问题

    $a = 0; switch($a){ case $a > 7: echo 234; break; case $a > 2: echo 4556; break; default: echo ...

  10. HTML5——7个最牛的HTML5移动开发框架

    月的iPhoneDevCamp上写成的.创建它的一个主要动力是基于一个几乎每一个单独的iPhone开发新手都要面对的简单事实:Objective-C是一个对Web开发人员来说非常陌生的环境,并且Web ...