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

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

 
题目意思:
有n个人,每个人都有自己的身高、性别、喜欢的音乐、喜欢的运动,当满足以上4个条件中至少一个条件的时候则两个人可以同时被选出来,问最多能选出多少人。
 
思路:
最大独立集,男生放在左边,女生放在右边,建二分图。ans=n-最大匹配数。
 
代码:

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 505 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} int n, m;
bool visited[N];
vector<int>ve[N];
int from[N]; struct node{
int h;
char sex[];
char music[];
char sport[];
}a[N]; int march(int u){
int i, j, k, v;
for(i=;i<ve[u].size();i++){
v=ve[u][i];
if(!visited[v]){
visited[v]=true;
if(from[v]==-||march(from[v])){
from[v]=u;
return ;
}
}
}
return ;
} main()
{
int t, i, j, k;
cin>>t;
while(t--){
scanf("%d",&n);
for(i=;i<n;i++){
scanf("%d%s%s%s",&a[i].h,a[i].sex,a[i].music,a[i].sport);
}
for(i=;i<=n;i++) ve[i].clear();
for(i=;i<n;i++){
if(a[i].sex[]=='F'){
for(j=;j<n;j++){
if(a[j].sex[]=='M'){
if(abs(a[i].h-a[j].h)<=&&strcmp(a[i].music,a[j].music)==&&strcmp(a[i].sport,a[j].sport)){
ve[i].push_back(j);
}
}
}
}
} int num=;
memset(from,-,sizeof(from));
for(i=;i<n;i++){
memset(visited,false,sizeof(visited));
if(march(i)) num++;
}
printf("%d\n",n-num);
}
}

POJ 2771 二分图(最大独立集)的更多相关文章

  1. Poj(2771),最大独立集

    题目链接:http://poj.org/problem?id=2771 Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K To ...

  2. Girls and Boys POJ - 1466 【(二分图最大独立集)】

    Problem DescriptionIn the second year of the university somebody started a study on the romantic rel ...

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

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

  4. POJ 3692 Kindergarten(二分图最大独立集)

    题意: 有G个女孩,B个男孩.女孩彼此互相认识,男孩也彼此互相认识.有M对男孩和女孩是认识的.分别是(g1,b1),.....(gm,bm). 现在老师要在这G+B个小孩中挑出一些人,条件是这些人都互 ...

  5. poj——2771 Guardian of Decency

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

  6. HDU 3829 - Cat VS Dog (二分图最大独立集)

    题意:动物园有n只猫和m条狗,现在有p个小孩,他们有的喜欢猫,有的喜欢狗,其中喜欢猫的一定不喜欢狗,喜欢狗的一定不喜欢猫.现在管理员要从动物园中移除一些动物,如果一个小孩喜欢的动物留了下来而不喜欢的动 ...

  7. HDU3829(KB10-J 二分图最大独立集)

    Cat VS Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)Total ...

  8. BZOJ3175:[TJOI2013]攻击装置(二分图最大独立集)

    Description 给定一个01矩阵,其中你可以在0的位置放置攻击装置.每一个攻击装置(x,y)都可以按照“日”字攻击其周围的 8个位置(x-1,y-2),(x-2,y-1),(x+1,y-2), ...

  9. [luoguP3355] 骑士共存问题(二分图最大独立集)

    传送门 模型 二分图最大独立集,转化为二分图最大匹配,从而用最大流解决. 实现 首先把棋盘黑白染色,使相邻格子颜色不同. 把所有可用的黑色格子看做二分图X集合中顶点,可用的白色格子看做Y集合顶点. 建 ...

随机推荐

  1. iOS 推送消息长度

    iOS最大推送消息长度 官方要求是256个字节 实际测试为1005个字节 在iPhone6上测试 锁屏时收到消息时只显示76个汉字,剩下的被隐藏 程序进入后台时只显示47个汉字,剩下的被隐藏

  2. HDU5807分段dp

    DAG图. [题意] n(50)个城市m(c(n,2))条单向边(x,y),保证x<y 对于三个点(x,y,z)如果abs(w[x]-w[y])<=K && abs(w[x ...

  3. zend studio12.5破解方法

    其实,很简单,找到zend studio 安装目录,G:\zend studio\plugins,把文件com.zend.verifier_12.5.1.v20150514-2003.jar替换成压缩 ...

  4. js optiontransferselect

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  5. Javascript链式调用案例

    jQuery用的就是链式调用.像一条连接一样调用方法. 链式调用的核心就是return this;,每个方法都返回对象本身. 下面是简单的模拟jQuery的代码, <script> win ...

  6. CSS 关于IE6 margin 为负数 负值的时候 正常显示的方法

    一定要加position: relative; 有时候比如margin-left的负数,还需要加上如 float:left 属性.

  7. hiho_1054_滑动解锁

    题目大意 智能手机九点屏幕滑动解锁,如果给出某些连接线段,求出经过所有给出线段的合法的滑动解锁手势的总数.题目链接: 滑动解锁 题目分析 首先,尝试求解没有给定线段情况下,所有合法的路径的总数.可以使 ...

  8. dshow,Sample Grabber 从摄像头采集

    char* CCameraDS::QueryFrame() { long evCode, size = 0; #if CALLBACKMODE static double lastSampleTime ...

  9. Animation用法

    测试代码及说明: <!DOCTYPE html> <html lang="en-US"> <head> <meta charset=&qu ...

  10. LinuxShell脚本攻略--第二章 命令之乐

    用 cat 进行拼接 文件查找与文件列表玩转 xargs 用 tr 进行转换排序临时文件命名与随机数分割文件和数据根据扩展名切分文件名mv 批量重命名文件交互输入自动化 cat: echo 'Text ...