题目链接: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 解题报告的更多相关文章

  1. poj——2771 Guardian of Decency

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

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

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

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

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

  4. POJ 2771 Guardian of Decency(最大独立集数=顶点数-最大匹配数)

    题目链接: http://poj.org/problem?id=2771 Description Frank N. Stein is a very conservative high-school t ...

  5. POJ 2771 Guardian of Decency

    http://poj.org/problem?id=2771 题意: 一个老师想带几个同学出去,但是他怕他们会谈恋爱,所以带出去的同学两两之间必须满足如下条件之一: ①身高差大于40  ②同性 ③喜欢 ...

  6. POJ 2771 Guardian of Decency(求最大点独立集)

    该题反过来想:将所有可能发生恋爱关系的男女配对,那么可以带出去的人数应该等于这个二分图的最大独立集 先要做一下预处理,把不符合要求的双方先求出来, company[i][j]表示i.j四个标准都不符合 ...

  7. UVA 12083 POJ 2771 Guardian of Decency

    /* http://acm.hust.edu.cn/vjudge/contest/view.action?cid=71805#problem/C */ 性质: [1]二分图最大点独立数=顶点数-二分图 ...

  8. poj 2771 Guardian of Decency(最大独立数)

    题意:人与人之间满足4个条件之一即不能成为一对(也就说这4个条件都不满足才能成为一对),求可能的最多的单身人数. 思路:把男女分为两部分,接下来就是二分图的匹配问题.把能成为一对的之间连边,然后求出最 ...

  9. 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 ...

随机推荐

  1. 巴蜀1088 Antiprime数

    Description 如果一个自然数n(n>=1),满足所有小于n的自然数(>=1)的约数个数都小于n的约数个数,则n是一个Antiprime数.譬如:1, 2, 4, 6, 12, 2 ...

  2. 【HDOJ5952】Counting Cliques(团,dfs)

    题意:给定一张n点m边的图,求大小为S的团的个数 N ≤ 100,M ≤ 1000,2 ≤ S ≤ 10,保证点的度不超过20 思路:dfs 因为每个点可能不止属于一个极大团,所以不能求出极大团然后计 ...

  3. 【BZOJ1412】狼和羊的故事(最小割)

    题意:将一个由0,1,2构成的矩阵里的1与2全部分割最少需要选取多少条边 n,m<=100 思路:裸的最小割模型 相邻的格子连容量为1的边(其实可以少连很多遍,1与1,2与2之间的边是没有意义的 ...

  4. Elasticsearch使用syslog发送Watcher告警事件

    https://blog.csdn.net/mvpboss1004/article/details/70158864?locationNum=9&fps=1

  5. hzwer与逆序对

    codevs——4163 hzwer与逆序对 貌似这个题和上个题是一样的((⊙o⊙)…)  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解       题目 ...

  6. 【mac】mac上安装软件,报错 鉴定错误,但是安装包都是好的

    出现这个问题, 原因解析: 不是你的安装包下载出错了或者下载失败了这种原因 而是你在打开这个安装包的时候,一定是让你输入密码,而你的密码没有输入正确 解决方式:重新开始打开这个软件的安装包 如下: 1 ...

  7. [WASM Rust] Create and Publish a NPM Package Containing Rust Generated WebAssembly using wasm-pack

    wasm-pack is a tool that seeks to be a one-stop shop for building and working with Rust generated We ...

  8. SODBASE CEP学习(四)续:类SQL语言EPL与Storm或jStorm集成-使用分布式缓存

    流式计算在一些情况下会用到分布式缓存,从而实现(1)想把统计或计算结果保存在分布缓存中.供其他模块或其他系统调用. (2)某一滑动时间窗体上计数.比如实时统计1小时每一个Cookie的訪问量.实时统计 ...

  9. 【转载】细聊分布式ID生成方法

    一.需求缘起 几乎所有的业务系统,都有生成一个记录标识的需求,例如: (1)消息标识:message-id (2)订单标识:order-id (3)帖子标识:tiezi-id 这个记录标识往往就是数据 ...

  10. android-custom-tab-with-viewpager

    https://github.com/eltld/android-custom-tab-with-viewpager