Guardian of Decency

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

 

Problem H - Guardian of Decency

Time limit: 15 seconds

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.

二分图最大匹配,使用匈牙利算法解决。

根据性别将学生分为两个不交集合。

求出最大匹配数m后,n - m即为所求。

AC Code:

 #include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define clr(a, i) memset(a, i, sizeof(a))
#define FOR0(i, n) for(int i = 0; i < n; ++i)
#define FOR1(i, n) for(int i = 1; i <= n; ++i)
#define sf scanf
#define pf printf const int MAXN = ;
struct Node
{
int h;
char gen;
char mus[];
char spo[];
void input() {
scanf("%d %c %s %s", &h, &gen, mus, spo);
}
bool satisfy(Node &y) const{
return gen != y.gen && fabs(h - y.h) <= && !strcmp(mus, y.mus) &&
strcmp(spo, y.spo);
}
}pup[MAXN];
int n;
vector<int> female, male;
bool adj[MAXN][MAXN];
int match[MAXN];
bool vis[MAXN]; void addEdge(int i)
{
adj[i][i] = false;
for (int j = ; j < i; ++j){
adj[i][j] = adj[j][i] = pup[i].satisfy(pup[j]);
}
} bool findCrossPath(int v)
{
for(int i = ; i < n; ++i)
{
if(adj[v][i] == true && vis[i] == false)
{
vis[i] = true;
if(match[i] == - || findCrossPath(match[i]))
{
match[i] = v;
return true;
}
}
}
return false;
} int Hungary()
{
int cnt = ;
memset(match, -, sizeof(match));
for(vector<int>::iterator it = male.begin(); it != male.end(); ++it)
{
memset(vis, false, sizeof(vis));
if(match[*it] == - && findCrossPath(*it))
{
++cnt;
}
}
return cnt;
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
female.clear();
male.clear();
scanf("%d", &n);
for(int i = ; i < n; ++i)
{
pup[i].input();
if(pup[i].gen == 'F') female.push_back(i);
else male.push_back(i);
addEdge(i);
}
printf("%d\n", n - Hungary());
}
return ;
}

Guardian of Decency(二分图)的更多相关文章

  1. UVA-12083 Guardian of Decency 二分图 最大独立集

    题目链接:https://cn.vjudge.net/problem/UVA-12083 题意 学校组织去郊游,选择最多人数,使得任意两个人之间不能谈恋爱 不恋爱条件是高差大于40.同性.喜欢的音乐风 ...

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

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

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

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

  4. poj——2771 Guardian of Decency

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

  5. POJ2771_Guardian of Decency(二分图/最大独立集=N-最大匹配)

    解决报告 http://blog.csdn.net/juncoder/article/details/38159017 题目传送门 题意: 看到题目我就笑了.., 老师觉得这种两个学生不是一对: 身高 ...

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

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

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

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

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

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

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

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

随机推荐

  1. 深究JS异步编程模型

    前言  上周5在公司作了关于JS异步编程模型的技术分享,可能是内容太干的缘故吧,最后从大家的表情看出"这条粉肠到底在说啥?"的结果:(下面是PPT的讲义,具体的PPT和示例代码在h ...

  2. Javascript中prototype属性的详解

    原文链接:http://www.cnblogs.com/Uncle-Keith/p/5834289.html 在典型的面向对象的语言中,如java,都存在类(class)的概念,类就是对象的模板,对象 ...

  3. Ubuntu SVN客户端安装

    查看系统版本: uname -a (Linux查看版本当前操作系统内核信息) cat /proc/version (Linux查看当前操作系统版本信息) 1.首先需要安装Ubuntu SVN.Ubun ...

  4. js不是从上到下执行的吗?

    如果说js是从上到下解释执行的, 那么,按道理应该会执行错误前面的代码. 如: [代码一] //输出1,2,到3报错 console.log("一") console.log(&q ...

  5. paip.取当天记录的方法sql跟hql hibernate

    paip.取当天记录的方法sql跟hql hibernate #------两个方法...函数法和日期计算法.. 函数法: DATEDIFF(d,createTime,GETDATE())=0   / ...

  6. Socket通信实例(C#)

    SOCKET原理 一.套接字(socket)概念 套接字(socket)是通信的基石,是支持TCP/IP协议的网络通信的基本操作单元.它是网络通信过程中端点的抽象表示,包含进行网络通信必须的五种信息: ...

  7. python实现curl功能

    之前写过一篇文章关于python CURL模块的,在这里我们从urllib来实现同样的功能.具体代码如下: import urllib import urllib2 import json #发起请求 ...

  8. [原创]Java性能优化权威指南读书思维导图

    [原创]Java性能优化权威指南读书思维导图 书名:Java性能优化权威指南 原书名:Java performance 作者: (美)Charlie Hunt    Binu John 译者: 柳飞 ...

  9. Spring3 整合Hibernate3.5 动态切换SessionFactory (切换数据库方言)

    一.缘由 上一篇文章Spring3.3 整合 Hibernate3.MyBatis3.2 配置多数据源/动态切换数据源 方法介绍到了怎么样在Sping.MyBatis.Hibernate整合的应用中动 ...

  10. 解放双手——Android自动化测试

    解放程序猿宝贵的右手(或者是左手) http://blog.csdn.net/eclipsexys/article/details/45622813 --Android自动化测试技巧 Google大神 ...