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 ...
随机推荐
- Linux java 启动脚本
#!/bin/bash export LANG=en_US.UTF8 start(){ ulimit -n 65535 #find the jars jar_lib=`ls -1 lib/*.jar` ...
- 标准C程序设计七---06
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- C++ 中new
operator new在C++中的各种写法 (2011-09-21 14:59:33) 标签: 杂谈 乍一看,在C++中动态分配内存很简单:new是分配,delete是释放,就这么简单.然而,这 ...
- 531. Lonely Pixel I
Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...
- bzoj 2694: Lcm
2694: Lcm Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 422 Solved: 220[Submit][Status][Discuss] ...
- mysql获取子父级节点
获取所有子节点 DROP FUNCTION IF EXISTS `F_Co29_GetAllChildrenIdsOfTaskevent`;DELIMITER //CREATE FUNCTION `F ...
- SQL2000数据库密码被替换,重置密码提示未能找到存储过程sp_password解决方案
利用windows身份验证进入查询分析器后在master数据库下运行如下脚本: create procedure sp_password @old sysname = NULL, -- the old ...
- flask结合令牌桶算法实现上传和下载速度限制
限流.限速: 1.针对flask的单个路由进行限流,主要场景是上传文件和下载文件的场景 2.针对整个应用进行限流,方法:利用nginx网关做限流 本文针对第一中情况,利用令牌桶算法实现: 这个方法:h ...
- 【剑指offer】打印1到最大的n位数
题目描写叙述: 输入数字n,按顺序打印出从1到最大的n位十进制数.比方输入3,则打印出1.2.3一直到最大的3位数即999. 分析描写叙述: 首先想到的是先计算出最大的n位数是多少,然后用一个循环从1 ...
- SpringBoot学习之快速入门创建
maven构建项目 1.访问http://start.spring.io/,进入快速创建工程的主页 可参考下图所示: 2.选择构建工具Maven Project.Spring Boot版本1.3.6以 ...