poj 2771 Guardian of Decency 解题报告
题目链接:http://poj.org/problem?id=2771
题目意思:有一个保守的老师要带他的学生来一次短途旅行,但是他又害怕有些人会变成情侣关系,于是就想出了一个方法:
1、身高差距 > 40cm
2、相同性别
3、喜欢的音乐种类 不同
4、有共同喜爱的 运动
只要满足其中这4个条件中的一个(当然越多越好啦),就可以将他们编为一组啦(一组两个人),求能被编为一组的最多组数。
这题实质上求的是二分图的最大独立集。 最大独立集 = 顶点数 - 最大匹配数
可以这样转化:两个人至少满足其中一个条件,那么就把四个条件都不满足的人连边(即配对),然后找出匹配数(也就是公式中的 最大匹配数),总人数减去,也就是答案。
lrj 版本(虽然有点长,不过代码很靓,美的享受,有赏心悦目之感)


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm> using namespace std; const int maxn = + ; struct BPM
{
int n, m;
int G[maxn][maxn];
int left[maxn];
bool T[maxn]; void init(int n, int m)
{
this->n = n;
this->m = m;
memset(G, , sizeof(G));
} bool match(int u)
{
for (int v = ; v < m; v++)
{
if (G[u][v] && !T[v])
{
T[v] = true;
if (left[v] == - || match(left[v]))
{
left[v] = u;
return true;
}
}
}
return false;
} int solve()
{
memset(left, -, sizeof(left));
int ans = ;
for (int u = ; u < n; u++)
{
memset(T, , sizeof(T));
if (match(u))
ans++;
}
return ans;
}
}; BPM solver; struct Student
{
int h;
string music, sport;
Student(int h, string music, string sport): h(h), music(music), sport(sport){}
}; bool conflict(const Student& a, const Student& b)
{
return (abs(a.h-b.h) <= && a.music == b.music && a.sport != b.sport);
} int main()
{
int T, n;
while (scanf("%d", &T) != EOF)
{
while (T--)
{
scanf("%d", &n);
vector<Student> male, female;
for (int i = ; i < n; i++)
{
int h;
string gender, music, sport;
cin >> h >> gender >> music >> sport;
if (gender[] == 'M')
male.push_back(Student(h, music, sport));
else
female.push_back(Student(h, music, sport));
}
int x = male.size();
int y = female.size();
solver.init(x, y);
for (int i = ; i < x; i++)
{
for (int j = ; j < y; j++)
{
if (conflict(male[i], female[j]))
solver.G[i][j] = ;
}
}
printf("%d\n", x + y - solver.solve());
}
}
return ;
}
这个是我的 (时间真心不敢恭维啊 [>_<])


之所以还要除以2,是因为 男生(假设为 X 集合)跟女生的集合(Y)都是1~n,左右对称啦!
这样虽然不需要额外知道分别的男女生是多少,但是因为贪方便,时间也用多了,尤其对于 n 比较大的情况来说。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std; const int maxn = + ;
const int N = + ;
int vis[maxn], match[maxn];
int map[maxn][maxn];
int n, maxmatch; struct node
{
int h;
char sex;
char music[N];
char sport[N];
}student[maxn]; int dfs(int x)
{
for (int i = ; i <= n; i++)
{
if (!vis[i] && map[x][i])
{
vis[i] = ;
if (!match[i] || dfs(match[i]))
{
match[i] = x;
return ;
}
}
}
return ;
} void Hungary()
{
maxmatch = ;
for (int i = ; i <= n; i++)
{
memset(vis, , sizeof(vis));
maxmatch += dfs(i);
}
} int main()
{
int T;
while (scanf("%d", &T) != EOF)
{
while (T--)
{
scanf("%d", &n);
for (int i = ; i <= n; i++)
cin >> student[i].h >> student[i].sex >> student[i].music >> student[i].sport;
memset(map, , sizeof(map));
memset(match, , sizeof(match));
for (int i = ; i <= n; i++)
{
for (int j = i+; j <= n; j++)
{
if (!(abs(student[i].h - student[j].h) > || student[i].sex == student[j].sex || strcmp(student[i].music, student[j].music)|| !strcmp(student[i].sport, student[j].sport)))
map[i][j] = map[j][i] = ;
}
}
Hungary();
printf("%d\n", n-(maxmatch>>));
}
}
return ;
}
poj 2771 Guardian of Decency 解题报告的更多相关文章
- poj——2771 Guardian of Decency
poj——2771 Guardian of Decency Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5916 ...
- 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 ...
- POJ 2771 Guardian of Decency(最大独立集数=顶点数-最大匹配数)
题目链接: http://poj.org/problem?id=2771 Description Frank N. Stein is a very conservative high-school t ...
- POJ 2771 Guardian of Decency
http://poj.org/problem?id=2771 题意: 一个老师想带几个同学出去,但是他怕他们会谈恋爱,所以带出去的同学两两之间必须满足如下条件之一: ①身高差大于40 ②同性 ③喜欢 ...
- POJ 2771 Guardian of Decency(求最大点独立集)
该题反过来想:将所有可能发生恋爱关系的男女配对,那么可以带出去的人数应该等于这个二分图的最大独立集 先要做一下预处理,把不符合要求的双方先求出来, company[i][j]表示i.j四个标准都不符合 ...
- UVA 12083 POJ 2771 Guardian of Decency
/* http://acm.hust.edu.cn/vjudge/contest/view.action?cid=71805#problem/C */ 性质: [1]二分图最大点独立数=顶点数-二分图 ...
- poj 2771 Guardian of Decency(最大独立数)
题意:人与人之间满足4个条件之一即不能成为一对(也就说这4个条件都不满足才能成为一对),求可能的最多的单身人数. 思路:把男女分为两部分,接下来就是二分图的匹配问题.把能成为一对的之间连边,然后求出最 ...
- POJ 2054 Color a Tree解题报告
题干 Bob is very interested in the data structure of a tree. A tree is a directed graph in which a spe ...
随机推荐
- ssh的安装和使用
1.ssh的安装 服务器端:sudo apt-get install openssh-serve 客户端:sudo apt-get install openssh-client 2.ssh的操作 查看 ...
- stm32f103定时器
1)TIM_TimeBaseInitTypeDef 时基初始化结构体,它包括了四个成员函数:TIM_ClockDivision.TIM_CounterMode.TIM_Period.TIM_Presc ...
- 大整数类BIGN的设计与实现 C++高精度模板
首先感谢刘汝佳所著的<算法竞赛入门经典>. 众所周知,C++中储存能力最大的unsigned long long 也是有着一个上限,如果我们想计算非常大的整数时,就不知所措了,所以,我写了 ...
- mysql中进行删除操作时用到not in 导致删除不成功
delete from tb_news where id not in ( select max(id) From tb_news Group By title ) 刚开始用这条语句删除一直不成功 然 ...
- Ubuntu 16.04下在Shell终端下使用nautilus快速打开窗口文件夹
Ubunut 16.04默认使用nautilus进行管理资源文件夹,nautilus默认是支持参数传递的. 使用: nautilus /dirurl 打开当前文件夹(可以使用$PWD代替): naut ...
- Java抓屏程序代码
原文:http://www.open-open.com/code/view/1422262655200 import java.awt.Dimension; import java.awt.Recta ...
- 我的javascript
JavaScript的简介: 1.定义:javascript是一门动态弱类型的解释型编程语言,增强页面动态效果,实现页面与用户之间的实时动态的交互. javascript是由三部分组成:ECMAScr ...
- 为什么说JAVA中要慎重使用继承 C# 语言历史版本特性(C# 1.0到C# 8.0汇总) SQL Server事务 事务日志 SQL Server 锁详解 软件架构之 23种设计模式 Oracle与Sqlserver:Order by NULL值介绍 asp.net MVC漏油配置总结
为什么说JAVA中要慎重使用继承 这篇文章的主题并非鼓励不使用继承,而是仅从使用继承带来的问题出发,讨论继承机制不太好的地方,从而在使用时慎重选择,避开可能遇到的坑. JAVA中使用到继承就会有两 ...
- C语言连接MySQL(codeblocks)
#include <stdio.h> #include <winsock2.h> #include <mysql.h> /*数据库连接用宏*/ #define HO ...
- 【手势交互】4. Kinect for XBox
"You are the Controller",Kinect for Xbox的广告词.明白说明了Kinect体感的交互方式.作为一款集成了诸多先进视觉技术的自然交互设备,Kin ...