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. Qt5.3 打印示例时出现错误

    说明:今天我在用Qt5.3写打印文档的时候,编译出错了,出错代码为: C:\Users\joe\Desktop\5-9\myPrint\mainwindow.cpp:35: error: undefi ...

  2. windows下安装PhpDocumentor(phpdoc)笔记

    PhpDocumentor简介 PHPDocumentor是一个用PHP写的工具,对于有规范注释的php程序,它能够快速生成具有相互参照,索引等功能的API文档.老的版本是phpdoc,从1.3.0开 ...

  3. 【Android】应用程序Activity启动过程分析

    在Android系统中,有两种操作会引发Activity的启动,一种用户点击应用程序图标时,Launcher会为我们启动应用程序的主Activity:应用程序的默认Activity启动起来后,它又可以 ...

  4. 判断输入的数是否为数字,不使用isNaN

    虽然不使用 isNaN ,但使用了 Math 的一些方法. <!-- Author: XiaoWen Create a file: 2016-12-08 11:14:34 Last modifi ...

  5. Android动画及图片的缩放和旋转

    Android动画有2种,一种是Tween Animation,另一种是Frame Animation,先说说Tween动画吧. Tween动画是对视图对象中的内容进行一系列简单的转换,比如位置的移动 ...

  6. 互信息(Mutual Information)

    本文根据以下参考资料进行整理: 1.维基百科:https://zh.wikipedia.org/wiki/%E4%BA%92%E4%BF%A1%E6%81%AF 2.新浪博客:http://blog. ...

  7. linux web服务器必需的库文件

    往往安装完linux之后,本文用的centos6.4,再编译安装其它服务器软件时,总是提示缺少各种库文件,在这里我总结了一下 平时web服务器经常需要的一些库,如下: yum -y install m ...

  8. zookeeper Watcher API 说明

    Watcher 在 ZooKeeper 是一个核心功能,Watcher 可以监控目录节点的数据变化以及子目录的变化,一旦这些状态发生变化,服务器就会通知所有设置在这个目录节点上的 Watcher,从而 ...

  9. Android防止按钮连续点击

    为了防止用户或者测试MM疯狂的点击某个button,写个方法防止按钮连续点击. public class Utils { private static long lastClickTime; publ ...

  10. Android开发:第四日——SQLite初接触

    一.SQLite 介绍 SQLite一个非常流行的轻量级嵌入式数据库,SQLite支持多数的SQL92标准,在一些场合下其性能优于MySql等数据库引擎,并且只利用很少的内存就有很好的性能.此外它还是 ...