Guardian of Decency

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 6133   Accepted: 2555

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个人,从中选出最多的人去旅游。当两个人可能成为couples时两个人不能同时去,可能成为couples的条件是:1、身高差小于等于40;2、不同性别;3、喜欢相同的音乐;4、喜欢不同的运动。

分析

如果两个人可能成为couples,连边,求最大点独立集。

二分图的最大点独立集 = n-最小点覆盖集=n-最大匹配数。

code

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream> using namespace std;
const int N = ;
const int INF = 1e9; struct Student{
int h;
string x,y,s;
}data[];
struct Edge{
int to,nxt,c;
Edge() {}
Edge(int x,int y,int z) {to = x,c = y,nxt = z;}
}e[];
int q[],L,R,S,T,tot = ;
int dis[N],cur[N],head[N]; void add_edge(int u,int v,int c) {
e[++tot] = Edge(v,c,head[u]);head[u] = tot;
e[++tot] = Edge(u,,head[v]);head[v] = tot;
}
bool bfs() {
for (int i=; i<=T; ++i) cur[i] = head[i],dis[i] = -;
L = ,R = ;
q[++R] = S;dis[S] = ;
while (L <= R) {
int u = q[L++];
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (dis[v] == - && e[i].c > ) {
dis[v] = dis[u]+;q[++R] = v;
if (v==T) return true;
}
}
}
return false;
}
int dfs(int u,int flow) {
if (u==T) return flow;
int used = ;
for (int &i=cur[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (dis[v] == dis[u] + && e[i].c > ) {
int tmp = dfs(v,min(flow-used,e[i].c));
if (tmp > ) {
e[i].c -= tmp;e[i^].c += tmp;
used += tmp;
if (used == flow) break;
}
}
}
if (used != flow) dis[u] = -;
return used;
}
int dinic() {
int ret = ;
while (bfs()) ret += dfs(S,INF);
return ret;
}
void Clear() {
tot = ;
memset(head,,sizeof(head));
}
int main() {
int Case,n;
scanf("%d",&Case);
while (Case--) {
Clear();
scanf("%d",&n);
S = n+n+;T = n+n+; //-
for (int i=; i<=n; ++i) {
scanf("%d",&data[i].h);
cin >> data[i].x >> data[i].y >> data[i].s;
}
for (int i=; i<=n; ++i)
for (int j=; j<=n; ++j) { // 可能成为couples的连边
if (abs(data[i].h-data[j].h) > ) continue;
if (data[i].x == data[j].x) continue;
if (data[i].y != data[j].y) continue;
if (data[i].s == data[j].s) continue;
add_edge(i,j+n,);
}
for (int i=; i<=n; ++i) add_edge(S,i,),add_edge(i+n,T,);
int ans = dinic();
printf("%d\n",n-ans/);
}
return ;
}

最新文章

  1. AndroidPn源码分析(一)
  2. Swift3.0语言教程字符串与文件的数据转换
  3. thumbnailator图片处理
  4. Task+http请求
  5. 带删除小图标的EditText
  6. IOS-day01_OC中类的创建以及使用
  7. Python关于eval与json在字典转换方面的性能比较
  8. makefile简单helloworld
  9. [LeetCode]题解(python):009-Palindrome Number
  10. POJ 3233 Matrix Power Series(矩阵高速功率+二分法)
  11. 在Visual Studio 2013 中使用C++单元测试
  12. LAMP与LNMP架构的区别及其具体的选择说明
  13. 大数据mapreduce俩表join之python实现
  14. git 常用命令收集
  15. 两数据库Dblink数据抽取blob
  16. Linux下软件包安装
  17. [PE484]Arithmetic Derivative
  18. [SQL] sql server中如何查看执行效率不高的语句
  19. MDK软件仿真常见问题
  20. Dozer 对象的&quot;搬运工&quot;

热门文章

  1. 使用AOP监控用户操作并插入数据库
  2. JQuery初识(二)
  3. vue-extend 选项
  4. 新客户上云 - 来自 Azure 技术支持部门的忠告
  5. 使用CSS设置Chrome打印背景色
  6. 美国移民局的I797表原件和I129表是什么呢
  7. [Asp.Net] Form验证中 user.identity为false
  8. IBM WebSphere MQ安装及配置详解
  9. java 的http请求方式:HttpURLConnection和HttpClient
  10. 学习Rust Book之写Cargo配置文件