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

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

代码如下:

# include<iostream>
# include<cstdio>
# include<string>
# include<vector>
# include<cstring>
# include<algorithm>
using namespace std;
# define REP(i,s,n) for(int i=s;i<n;++i)
# define CL(a,b) memset(a,b,sizeof(a))
# define CLL(a,b,n) fill(a,a+n,b) const int N=505;
struct student
{
int high;
char sex;
string music,PE;
};
student stu[N];
int n,vis[N],link[N];
vector<int>G[N]; bool ok(int i,int j)
{
if(stu[i].sex==stu[j].sex) return false;
if(stu[i].PE==stu[j].PE) return false;
if(abs(stu[i].high-stu[j].high)>40) return false;
if(stu[i].music!=stu[j].music) return false;
return true;
} bool dfs(int x)
{
REP(i,0,G[x].size()){
int y=G[x][i];
if(vis[y]) continue;
vis[y]=1;
if(link[y]==-1||dfs(link[y])){
link[y]=x;
return true;
}
}
return false;
} int match()
{
int res=0;
CL(link,-1);
REP(i,1,n+1){
CL(vis,0);
if(dfs(i)) ++res;
}
return res;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
REP(i,1,n+1) G[i].clear();
REP(i,1,n+1) cin>>stu[i].high>>stu[i].sex>>stu[i].music>>stu[i].PE;
REP(i,1,n+1){
REP(j,i+1,n+1) if(ok(i,j)){
G[i].push_back(j);
G[j].push_back(i);
}
}
int ans=match();
printf("%d\n",n-ans/2);
}
return 0;
}

  

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. VirtualBox vbox not found

    VirtualBox vbox file not found Problem When I opened virtualbox, Today, it showed "inaccessible ...

  2. 商铺项目(使用DES加密配置信息)

    package com.ouyan.o2o.util; import java.security.Key; import java.security.SecureRandom; import java ...

  3. mysql 约束条件 auto_increment 自动增长起始值 布长 起始偏移量

    我们指定一个字段为自动增长,他默认从1开始自动增长,默认值为1,每次增长为1,步长为1 模糊查询 like % 代表任意个数字符 任意字符长度 查看mysql正在使用变量 show variables ...

  4. placement new--《C++必知必会》 条款35

    placement new是重载operator new的一个标准.全局的版本,它不能被自定义的版本代替(不像普通的operator new和operator delete能够被替换成用户自定义的版本 ...

  5. Linux 中的 Service

    参考: cnblogs.com/xiaofan21 - linux service和daemon cnblogs.com/xuange306 - linux service命令常见使用方法 cnblo ...

  6. 计算机bit是什么意思

    bit是计算机中数据的最小单位,即二进制位,数字0和1 一个字节是八位(8个0和1 或 1 组成的一串二进制) 一个字是16位,等于2个字节 用八位二进制表示的字符叫单字节字符, 用16位二进制数表示 ...

  7. 怎样在linux下对U盘进行格式化和分区

    说明,为了不做无用功,首先必须卸载要分区的设备,分区才能执行成功.通过命令umount /media/?? 或者umount /mnt/??? 看你的实际情况,这一步必不可少.1.首先通过命令fdis ...

  8. Maven聚合项目在eclipse中显示没有层次

    大部分时间都在用idea做maven的项目,今天用eclipse导入了maven项目,果然不出所料,界面有显示问题,各个模块都堆叠在同一层级,根本看不出父项目与子项目之间的层次关系,见下图: 于是找修 ...

  9. JS中的slice和splice

    1,slice  : 定义:接收一个或两个参数,它可以创建一个由当前数组中的一项或多项组成的新数组,注意是新数组哦~ 也就是说它不会修改原来数组的值. 用法:slice( para1 ),会截取从pa ...

  10. js 变量、函数提升 与js的预编译有关

    参考网址:http://www.codesec.net/view/178491.html 先简单理解下作用域的概念,方便对变量与函数提升的概念的理解 function foo() { var x = ...