题目链接: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. 使用putty上传下载文件(pscp)

    putty作为ssh工具开源免费,简单易用.可是如何使用它来上传和下载文件呢?答案在于pscp. pscp下载地址:http://www.chiark.greenend.org.uk/~sgtatha ...

  2. 看 nova

    本节重点介绍 nova-scheduler 的调度机制和实现方法:即解决如何选择在哪个计算节点上启动 instance 的问题. 创建 Instance 时,用户会提出资源需求,例如 CPU.内存.磁 ...

  3. 【BZOJ2049】洞穴勘测(LCT)

    题意:一张图,要求支持以下操作: 1.加边 2.删边 3.询问两点之间是否联通 100%的数据满足n≤10000, m≤200000 思路:LCT裸题,不需要维护任何信息 ..,..]of longi ...

  4. hzwer与逆序对

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

  5. 前端MVC Vue2学习总结(八)——前端路由

    路由是根据不同的 url 地址展示不同的内容或页面,早期的路由都是后端直接根据 url 来 reload 页面实现的,即后端控制路由. 后来页面越来越复杂,服务器压力越来越大,随着AJAX(异步刷新技 ...

  6. Go -- 实现二叉搜索树

    树: https://suanfa.herokuapp.com/3%E6%A0%91/binarytree/ 数据结构 首先我们定义需要的数据结构.注意,TreeNode的左右节点都是*TreeNod ...

  7. [转]JVM堆和栈的区别

    物理地址 堆的物理地址分配对对象是不连续的.因此性能慢些.在GC的时候也要考虑到不连续的分配,所以有各种算法.比如,标记-消除,复制,标记-压缩,分代(即新生代使用复制算法,老年代使用标记——压缩) ...

  8. 线段树区间更新,区间统计+离散化 POJ 2528 Mayor&#39;s posters

    题意:有一个非常长的板子(10000000长),在上面贴n(n<=10000)张海报.问最后从外面能看到几张不同的海报. 由于板子有10000000长,直接建树肯定会爆,所以须要离散化处理,对于 ...

  9. Effective C++ 条款六 若不想使用编译器自动生成的函数,就该明确拒绝

    class HomeForSale //防止别人拷贝方法一:将相应的成员函数声明为private并且不予实现 { public: private: HomeForSale(const HomeForS ...

  10. 线程池实例:使用Executors和ThreadPoolExecutor

    线程池负责管理工作线程,包含一个等待执行的任务队列.线程池的任务队列是一个Runnable集合,工作线程负责从任务队列中取出并执行Runnable对象. java.util.concurrent.ex ...