题目大意:一个老师要带一些学生去春游,但是要带的学生中任意两个人都满足下面四个条件中的至少一个:1、性别相同;2、身高差大与40公分;3、最喜欢的音乐类型不同;4、最喜欢的体育运动相同。问老师最多能带多少个学生?

题目分析:最大独立集问题。最大独立集+最小覆盖集=全集。将学生视为节点,对于任意两个不满足上述四个条件中的学生,连一条有向边。这就意味着一条边的两个端点至少有一个不能去春游,这与一条边中至少有一个去春游恰好互补,后者正是最小覆盖问题。

代码如下:

  1. # include<iostream>
  2. # include<cstdio>
  3. # include<string>
  4. # include<vector>
  5. # include<cstring>
  6. # include<algorithm>
  7. using namespace std;
  8. # define REP(i,s,n) for(int i=s;i<n;++i)
  9. # define CL(a,b) memset(a,b,sizeof(a))
  10. # define CLL(a,b,n) fill(a,a+n,b)
  11.  
  12. const int N=505;
  13. struct student
  14. {
  15. int high;
  16. char sex;
  17. string music,PE;
  18. };
  19. student stu[N];
  20. int n,vis[N],link[N];
  21. vector<int>G[N];
  22.  
  23. bool ok(int i,int j)
  24. {
  25. if(stu[i].sex==stu[j].sex) return false;
  26. if(stu[i].PE==stu[j].PE) return false;
  27. if(abs(stu[i].high-stu[j].high)>40) return false;
  28. if(stu[i].music!=stu[j].music) return false;
  29. return true;
  30. }
  31.  
  32. bool dfs(int x)
  33. {
  34. REP(i,0,G[x].size()){
  35. int y=G[x][i];
  36. if(vis[y]) continue;
  37. vis[y]=1;
  38. if(link[y]==-1||dfs(link[y])){
  39. link[y]=x;
  40. return true;
  41. }
  42. }
  43. return false;
  44. }
  45.  
  46. int match()
  47. {
  48. int res=0;
  49. CL(link,-1);
  50. REP(i,1,n+1){
  51. CL(vis,0);
  52. if(dfs(i)) ++res;
  53. }
  54. return res;
  55. }
  56.  
  57. int main()
  58. {
  59. int T;
  60. scanf("%d",&T);
  61. while(T--)
  62. {
  63. scanf("%d",&n);
  64. REP(i,1,n+1) G[i].clear();
  65. REP(i,1,n+1) cin>>stu[i].high>>stu[i].sex>>stu[i].music>>stu[i].PE;
  66. REP(i,1,n+1){
  67. REP(j,i+1,n+1) if(ok(i,j)){
  68. G[i].push_back(j);
  69. G[j].push_back(i);
  70. }
  71. }
  72. int ans=match();
  73. printf("%d\n",n-ans/2);
  74. }
  75. return 0;
  76. }

  

UVALive-3415 Guardian of Decency (最大独立集)的更多相关文章

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

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

  2. uvalive 3415 Guardian Of Decency

    题意: 有一个老师想组织学生出去旅游,为了避免他们之间有情侣产生,他制定了一系列的条件,满足这些条件之一,那么这些人理论上就不会成为情侣: 身高相差40cm:性别相同:喜欢的音乐风格不同:最喜欢的运动 ...

  3. UVALive3415 Guardian of Decency —— 最大独立集

    题目链接:https://vjudge.net/problem/UVALive-3415 题解: 题意:选出尽可能多的人, 使得他(她)们之间不会擦出火花.即求出最大独立集. 1.因为性别有男女之分, ...

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

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

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

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

  6. Guardian of Decency(二分图)

    Guardian of Decency Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submi ...

  7. 训练指南 UVALive - 3415(最大点独立集)

    layout: post title: 训练指南 UVALive - 3415(最大点独立集) author: "luowentaoaa" catalog: true mathja ...

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

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

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

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

  10. poj——2771 Guardian of Decency

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

随机推荐

  1. Instagram 架构分析笔记(转)

    原文:http://dbanotes.net/?s=Instagram+%E6%9E%B6%E6%9E%84%E5%88%86%E6%9E%90%E7%AC%94%E8%AE%B0 作者:冯大辉 In ...

  2. java-mybaits-00801-逆向工程

    1.1     什么是逆向工程 参看地址:http://www.mybatis.org/generator/index.html 使用官方网站的mapper自动生成工具mybatis-generato ...

  3. git-【七】bug分支

    在开发中,会经常碰到bug问题,那么有了bug就需要修复,在Git中,分支是很强大的,每个bug都可以通过一个临时分支来修复,修复完成后,合并分支,然后将临时的分支删除掉. 比如我在开发中接到一个40 ...

  4. PhotoSwipe中文API(一)

    入门 您应知道之前先做起事情: 1. PhotoSwipe不是一个简单的jQuery插件,至少基本的JavaScript知识才能安装. 2. PhotoSwipe需要预定义的图像尺寸(更多关于这一点) ...

  5. java.lang.UnsatisfiedLinkError: org.apache.hadoop.util.NativeCrc32.nativeComputeChunkedSumsByteArray(II[BI[BIILjava/lang/String;JZ)V

    环境: Spark2.1.0 .Hadoop-2.7.5   代码运行系统:Win 7在运行Spark程序写出文件(savaAsTextFile)的时候,我遇到了这个错误: // :: ERROR U ...

  6. Kafka丢失数据问题优化总结

    数据丢失是一件非常严重的事情事,针对数据丢失的问题我们需要有明确的思路来确定问题所在,针对这段时间的总结,我个人面对kafka 数据丢失问题的解决思路如下: 是否真正的存在数据丢失问题,比如有很多时候 ...

  7. 1.1 、Django 后台

    Django 后台 与后台相关文件:每个app中的 admin.py 文件与后台相关. 一,新建一个 名称为 HelloDjango 的项目 django-admin.py startproject ...

  8. [ 翻译]ruby rails相关的常见服务器

    原文:http://stackoverflow.com/questions/4113299/ruby-on-rails-server-options     一,Apache vs Nginx     ...

  9. MySQL从删库到跑路(三)——SQL语言

    作者:天山老妖S 链接:http://blog.51cto.com/9291927 一.SQL语言简介 1.SQL语言简介 SQL是结构化查询语言(Structured Query Language) ...

  10. 日志处理(二) 日志组件logback的介绍及配置使用方法(转)

    本文转自:http://www.cnblogs.com/yuanermen/archive/2012/02/13/2348942.html http://www.cnblogs.com/yuanerm ...