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. 第29题:LeetCode54:Spiral Matrix螺旋矩阵

    给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ...

  2. Mybatis中的增删改查

    相比jdbc mybatis在操作数据库方面比jdbc节省了大量的代码,及大量的代码冗余.使得操作起来更加简洁. 在Mapper中分别有着 select,insert, update,delete的这 ...

  3. PHP操作redis的常用例子

    Redis常用的例子 1,connect 描述:实例连接到一个Redis. 参数:host: string,port: int 返回值:BOOL 成功返回:TRUE;失败返回:FALSE 示例: &l ...

  4. 22.VUE学习之-replice删除当前评论条数

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. kali下将Python2.x切换至Python3.x

    注:我是将Python2切换到Python3.6版本的,下面文件夹名,请注意变更. 1.首先在/usr/local/下创建一个Python-3.6 注意文件夹名(根建议据自己安装版本命名)mkdir ...

  6. 服务器常说的U是什么意思?

    U是英文单词:unit 所说的1U和2U,是服务器的厚度,1U大概是相当于机柜的两个小格子,2U是四个格子.1U大概是4.45厘米(1U=1.75英寸,1英寸=2.54CM).以下这个是图片:

  7. 893E - Counting Arrays

    E. Counting Arrays time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  8. HDU1272小希的迷宫

    小希的迷宫 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一 ...

  9. SharedPreferences使用(通过键值保存数据)

    保存数据到SharedPreferences中 要想使用SharedPreferences来存储数据, 首先需要获取到SharedPreferences对象. Android中主要提供了三种方法用于得 ...

  10. UVa 1455 Kingdom 线段树 并查集

    题意: 平面上有\(n\)个点,有一种操作和一种查询: \(road \, A \, B\):在\(a\),\(b\)两点之间加一条边 \(line C\):询问直线\(y=C\)经过的连通分量的个数 ...