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:

  1. #include <iostream>
  2. #include <queue>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <cmath>
  6. using namespace std;
  7. #define clr(a, i) memset(a, i, sizeof(a))
  8. #define FOR0(i, n) for(int i = 0; i < n; ++i)
  9. #define FOR1(i, n) for(int i = 1; i <= n; ++i)
  10. #define sf scanf
  11. #define pf printf
  12.  
  13. const int MAXN = ;
  14. struct Node
  15. {
  16. int h;
  17. char gen;
  18. char mus[];
  19. char spo[];
  20. void input() {
  21. scanf("%d %c %s %s", &h, &gen, mus, spo);
  22. }
  23. bool satisfy(Node &y) const{
  24. return gen != y.gen && fabs(h - y.h) <= && !strcmp(mus, y.mus) &&
  25. strcmp(spo, y.spo);
  26. }
  27. }pup[MAXN];
  28. int n;
  29. vector<int> female, male;
  30. bool adj[MAXN][MAXN];
  31. int match[MAXN];
  32. bool vis[MAXN];
  33.  
  34. void addEdge(int i)
  35. {
  36. adj[i][i] = false;
  37. for (int j = ; j < i; ++j){
  38. adj[i][j] = adj[j][i] = pup[i].satisfy(pup[j]);
  39. }
  40. }
  41.  
  42. bool findCrossPath(int v)
  43. {
  44. for(int i = ; i < n; ++i)
  45. {
  46. if(adj[v][i] == true && vis[i] == false)
  47. {
  48. vis[i] = true;
  49. if(match[i] == - || findCrossPath(match[i]))
  50. {
  51. match[i] = v;
  52. return true;
  53. }
  54. }
  55. }
  56. return false;
  57. }
  58.  
  59. int Hungary()
  60. {
  61. int cnt = ;
  62. memset(match, -, sizeof(match));
  63. for(vector<int>::iterator it = male.begin(); it != male.end(); ++it)
  64. {
  65. memset(vis, false, sizeof(vis));
  66. if(match[*it] == - && findCrossPath(*it))
  67. {
  68. ++cnt;
  69. }
  70. }
  71. return cnt;
  72. }
  73.  
  74. int main()
  75. {
  76. int t;
  77. scanf("%d", &t);
  78. while(t--)
  79. {
  80. female.clear();
  81. male.clear();
  82. scanf("%d", &n);
  83. for(int i = ; i < n; ++i)
  84. {
  85. pup[i].input();
  86. if(pup[i].gen == 'F') female.push_back(i);
  87. else male.push_back(i);
  88. addEdge(i);
  89. }
  90. printf("%d\n", n - Hungary());
  91. }
  92. return ;
  93. }

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. (转)雅虎WEB前端网站优化 -- 34条军规

    雅虎给出了优化网站加载速度的34条法则(包括Yslow规则22条) 详细说明,下载转发 ponytail 的译文(来自帕兰映像). 1.Minimize HTTP Requests 减少HTTP请求 ...

  2. jquery时间倒计时

    代码: js: function countDown(time, id) {  //time的格式yyyy/MM/dd hh:mm:ss    var day_elem = $(id).find('. ...

  3. static/final

  4. Leetcode 66 Plus One STL

    题意让大数加1 我的做法是先让个位+1,再倒置digits,然后进位,最后倒置digits,得到答案. class Solution { public: vector<int> plusO ...

  5. SQL Server如何提高数据库备份的速度

    对于一个数据库完整备份来说,备份的速度很大程度上取决于下面两个因素:读磁盘数据.日志文件的吞吐量,写磁盘数据文件的吞吐量. 下图是备份过程中磁盘的变化情况: 读吞吐量 读吞吐量的大小取决于磁盘读取数据 ...

  6. ASP 中 Cookies 的 Expires 属性的设置(JS版本)

    直接上代码,代码中有注释 <%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%> <% var numVisi ...

  7. 关于ORACLE的重启命令

    有时候数据死了,或者没有监听,不必重启机子,重启一下数据库服务行了,下面是linux下的启动步骤,没有在win下执行过,可能也可以!以oracle9i为例(1) 以oracle身份登录数据库,命令:s ...

  8. 私有云android客户端2.1.2最新版本(ownCloud简体中文优化版)

    通过安装Ubuntu16.04+LAMP+ownCloud9.1+SSL建立私有云,下载ownCloud android客户端最新源码,针对国际语言简体中文化,修改部分代码,并进行补充.优化,编译生成 ...

  9. 解决genemotion模拟器冲突导致的Android Studio无法启动ADB的问题

    首先命令行下运行 adb nodaemon server ./adb nodaemon server (Mac OSX) 如果出现错误: error: could not install *smart ...

  10. 用户管理 之 Linux 用户(User)查询篇

    用户(User)和用户组(Group)的配置文件,是系统管理员最应该了解和掌握的系统基础文件之一,从另一方面来说,了解这些文件也是系统安全管理的重要组成部份:做为一个合格的系统管理员应该对用户和用户组 ...