uvalive 3415 Guardian Of Decency
题意:
有一个老师想组织学生出去旅游,为了避免他们之间有情侣产生,他制定了一系列的条件,满足这些条件之一,那么这些人理论上就不会成为情侣:
身高相差40cm;性别相同;喜欢的音乐风格不同;最喜欢的运动相同。
给出若干个学生的身高,性别,喜欢的音乐风格和喜欢的运动,问最多有多少人可以出去。
思路:
对于两个人,如果以上所有条件都不满足,那么就在他们之间连边,说明他们不能同时去。
问题就转化成了求一个二分图的最大独立集。(独立集是两两之间不存在边的点的集合,最大顾名思义)
最大独立集 = 点数- 最小点覆盖 = 点数 – 最大匹配
匈牙利算法,复杂度O(n^3)。
代码:
#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#include <vector>
using namespace std; const int N = ; vector<int> g[N];
int link[N];
bool vis[N]; struct node
{
int hei;
string sex,music,sport; node(int a,string b,string c,string d)
{
hei = a;
sex = b;
music = c;
sport = d;
}
}; vector<node> ns; bool dfs(int u)
{
for (int i = ;i < g[u].size();i++)
{
int v = g[u][i]; if (!vis[v])
{
vis[v] = ; if (link[v] == - || dfs(link[v]))
{
link[v] = u;
link[u] = v; return true;
}
}
} return false;
}
int solve(int n)
{
int cnt = ; memset(link,-,sizeof(link)); for (int i = ;i < n;i++)
{
if (link[i] == -)
{
memset(vis,,sizeof(vis));
if (dfs(i)) cnt++;
}
} return cnt;
} int mabs(int x)
{
return x >= ? x : -x;
} int main()
{
int t; scanf("%d",&t); while (t--)
{
int n; scanf("%d",&n); ns.clear(); for (int i = ;i < n;i++)
{
g[i].clear();
int a;
string b,c,d; cin >> a >> b >> c >> d; ns.push_back(node(a,b,c,d));
} for (int i = ;i < n;i++)
{
for (int j = i + ;j < n;j++)
{
bool f = ; if (mabs(ns[i].hei - ns[j].hei) > ) f = ;
if (ns[i].sex == ns[j].sex) f = ;
if (ns[i].music != ns[j].music) f = ;
if (ns[i].sport == ns[j].sport) f = ; if (!f)
{
g[i].push_back(j);
g[j].push_back(i);
}
}
} int ans = solve(n); printf("%d\n",n - ans);
} return ;
}
uvalive 3415 Guardian Of Decency的更多相关文章
- UVALive 3415 Guardian of Decency(二分图的最大独立集)
题意:老师在选择一些学生做活动时,为避免学生发生暧昧关系,就提出了四个要求.在他眼中,只要任意两个人符合这四个要求之一,就不可能发生暧昧.现在给出n个学生关于这四个要求的信息,求老师可以挑选出的最大学 ...
- Guardian of Decency UVALive - 3415 最大独立集=结点数-最大匹配数 老师带大学生旅游
/** 题目:Guardian of Decency UVALive - 3415 最大独立集=结点数-最大匹配数 老师带大学生旅游 链接:https://vjudge.net/problem/UVA ...
- Guardian of Decency(二分图)
Guardian of Decency Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submi ...
- 训练指南 UVALive - 3415(最大点独立集)
layout: post title: 训练指南 UVALive - 3415(最大点独立集) author: "luowentaoaa" catalog: true mathja ...
- POJ 2771 Guardian of Decency 【最大独立集】
传送门:http://poj.org/problem?id=2771 Guardian of Decency Time Limit: 3000MS Memory Limit: 65536K Tot ...
- POJ 2771 Guardian of Decency (二分图最大点独立集)
Guardian of Decency Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6133 Accepted: 25 ...
- uva 12083 Guardian of Decency (二分图匹配)
uva 12083 Guardian of Decency Description Frank N. Stein is a very conservative high-school teacher. ...
- poj——2771 Guardian of Decency
poj——2771 Guardian of Decency Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5916 ...
- Guardian of Decency UVALive - 3415(最大独立集板题)
老师在选择一些学生做活动时,为避免学生发生暧昧关系,就提出了四个要求.在他眼中,只要任意两个人符合这四个要求之一,就不可能发生暧昧.现在给出n个学生关于这四个要求的信息,求老师可以挑选出的最大学生数量 ...
随机推荐
- 2018/04/25 基于 编译安装的 PHP7 安装 swoole 扩展
在上一篇文章我们知道了如何去编译安装一个自己需要的 PHP 版本. 2018/04/25 PHP7的编译安装 这里还没有完,我们还需要安装我们的扩展,才算完成今天的任务. -- 下载扩展 还是官网下载 ...
- python编码类型互转总结
1.只有在unicode下才能将utf-8与gbk互转2.unicode是在内存中使用,bytes是文件存储和网络传输时使用-------------------------------------- ...
- Python+Flash+NodeJS 接口自动化平台
一.前端安装步骤# manager-web(1)下载项目 git clone https://github.com/t880216t/manager-web.git (2) 安装依赖 cnpm ins ...
- 那些年读过的书《Java并发编程实战》十、再探究Java内存模型
1.什么是内存模型,为什么需要它? (1)内存模型的发展背景 近几年计算性能通过重排序实现了很大的提升,而且处理器也越来越朝着多核处理器发展以实现硬件的并行性.随着处理器的不断强大,编译器也在不断的改 ...
- JavaScript字符串String
JavaScript中String类型用于表示由零个或者多个16位Unicode字符组成的字符序列即字符串:同时字符串可以用单引号或双引号表示. 下面是一些特殊的字面量: 字面量 含义\n 换行\t ...
- caz,数字证书,公钥
如何有效检查证书有效性? https://www.jianshu.com/p/f4a37da10c53 自签名的https证书是不安全的 https://www.cnblogs.com/liyy201 ...
- MySQL crash-safe replication【转载】
本文来自david大神的博客,innodb技术内幕的作者. http://insidemysql.blog.163.com/blog/static/202834042201385190333/ MyS ...
- 报错解决——Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
在导入tensorflow后,进行运算时,出现了报错Your CPU supports instructions that this TensorFlow binary was not compile ...
- vue中computed和watch的用法
computed用来监控自己定义的变量,该变量不在data里面声明,直接在computed里面定义,然后就可以在页面上进行双向数据绑定展示出结果或者用作其他处理: computed比较适合对多个变量或 ...
- [django]session设置与获取原理
admin登录 情况1: 登录后会产生一个sessionid 情况2: 自定义设置了key后,会多一个sessionid, 登录后会替换为登录后的sessionid的key值 if username ...