HDU 3829 Cat VS Dog / NBUT 1305 Cat VS Dog(二分图最大匹配)
HDU 3829 Cat VS Dog / NBUT 1305 Cat VS Dog(二分图最大匹配)
Description
The zoo have N cats and M dogs, today there are P children visiting the zoo, each child has a like-animal and a dislike-animal, if the child's like-animal is a cat, then his/hers dislike-animal must be a dog, and vice versa.
Now the zoo administrator is removing some animals, if one child's like-animal is not removed and his/hers dislike-animal is removed, he/she will be happy. So the administrator wants to know which animals he should remove to make maximum number of happy children.
Input
The input file contains multiple test cases, for each case, the first line contains three integers N <= 100, M <= 100 and P <= 500.
Next P lines, each line contains a child's like-animal and dislike-animal, C for cat and D for dog. (See sample for details)
Output
For each case, output a single integer: the maximum number of happy children.
Sample Input
1 1 2
C1 D1
D1 C1
1 2 4
C1 D1
C1 D1
C1 D2
D2 C1
Sample Output
1
3
Http
HDU:https://vjudge.net/problem/HDU-3829
NBUT:https://vjudge.net/problem/NBUT-1305
Source
二分图最大匹配
题目大意
在动物园中有n只猫和m只狗,现在来了P个小朋友,每个小朋友喜欢一只动物,不喜欢另一只动物,现在要选择移走若干只动物,若能满足喜欢的动物在而不喜欢的动物被移走,则该小朋友是开心的。现在求最多能使多少位小朋友开心。
解决思路
二分图的最大匹配问题,这里用匈牙利算法解决,算法内容可以参照我的这几篇博客:
http://www.cnblogs.com/SYCstudio/p/7138221.html
http://www.cnblogs.com/SYCstudio/p/7138230.html
那么笔者在本文就讲一讲将匈牙利算法运用在本题时,如何建图。
首先我们观察到,如果建图小朋友->动物无疑会增加算法的思考难度(反正笔者是没有想出来怎么做),那么我们就把每个小朋友i拆成两个点,开心的小朋友i和不开心的小朋友i',如果发现两个小朋友的动物需求有冲突(即i喜欢的是j不喜欢的,或i不喜欢的是j喜欢的),那么连边这两个小朋友i->j',j->i'(怎么有种回到了2-sat问题的感觉,但这和2-sat毫无关系)。然后对这个图跑一边匈牙利。
那么这时得到的最大匹配就是题目中要求的东西吗?显然不是。首先因为我们把一个点拆成了两个,这个答案要除以2;其次我们建的图是不开心的图,而题目要求的是开心的小朋友的数量,所以要用总人数P来减去除以2后的答案。这样我们就得到最终的解啦!
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxN=200;
const int maxP=1001;
const int inf=2147483647;
class Child
{
public:
int like,dislike;
};
int n,m,P;
Child C[maxP];
vector<int> E[maxP];
int Match[maxP];
bool vis[maxP];
bool Hungary(int u);
int main()
{
while (cin>>n>>m>>P)
{
memset(Match,-1,sizeof(Match));
for (int i=1;i<=P;i++)
E[i].clear();
for (int i=1;i<=P;i++)//这里把猫和狗的编号处理了一下,猫的编号为1~n,狗的编号为n+1~n+m
{
char ch;
cin>>ch;
int k=(ch=='C') ? 0 : 1;
int x;
cin>>x;
C[i].like=x+n*k;
cin>>ch;
k=(ch=='C') ? 0 : 1;
cin>>x;
C[i].dislike=x+n*k;
}
for (int i=1;i<=P;i++)//看看哪几对小朋友会冲突,建图
for (int j=i+1;j<=P;j++)
if ((C[i].like==C[j].dislike)||(C[i].dislike==C[j].like))
{
E[i].push_back(j);
E[j].push_back(i);
}
int Ans=0;
for (int i=1;i<=P;i++)//匈牙利算法
{
memset(vis,0,sizeof(vis));
if (Hungary(i))
Ans++;
}
cout<<P-Ans/2<<endl;
}
}
bool Hungary(int u)//匈牙利算法
{
for (int i=0;i<E[u].size();i++)
{
int v=E[u][i];
if (vis[v]==0)
{
vis[v]=1;
if ((Match[v]==-1)||(Hungary(Match[v])))
{
Match[v]=u;
return 1;
}
}
}
return 0;
}
HDU 3829 Cat VS Dog / NBUT 1305 Cat VS Dog(二分图最大匹配)的更多相关文章
- HDU 3829——Cat VS Dog——————【最大独立集】
Cat VS Dog Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit S ...
- HDU 3829
http://acm.hdu.edu.cn/showproblem.php?pid=2970 P个小朋友喜欢猫讨厌狗,喜欢狗讨厌猫,移除一定数量的猫狗,使开心的小朋友数量最多 二分图最大独立集=顶点数 ...
- HDU:过山车(二分图最大匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=2063 题意:有m个男,n个女,和 k 条边,求有多少对男女可以搭配. 思路:裸的二分图最大匹配,匈牙利算法. 枚 ...
- [HDU] 2063 过山车(二分图最大匹配)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2063 女生为X集合,男生为Y集合,求二分图最大匹配数即可. #include<cstdio> ...
- HDU 2255 奔小康赚大钱(带权二分图最大匹配)
HDU 2255 奔小康赚大钱(带权二分图最大匹配) Description 传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子. 这可是一件大事,关系到人民的住房问题啊 ...
- HDU 2063 过山车(模板—— 二分图最大匹配问题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2063 解题思路: 二分图最大匹配模板题. AC代码: #include<stdio.h> ...
- HDU 1045 - Fire Net - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- # 匈牙利算法(二分图最大匹配)- hdu 过山车
匈牙利算法(二分图最大匹配)- hdu 过山车 Hdu 2063 二分图:图中的点可以分成两组U,V,所有边都是连接U,V中的顶点.等价定义是:含奇数条边的图. 匹配:一个匹配是一个边的集合,其中任意 ...
- HDU 2444 The Accomodation of Students【二分图最大匹配问题】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2444 题意:首先判断所有的人可不可以分成互不认识的两部分.如果可以分成 ,则求两部分最多相互认识的对数. ...
随机推荐
- nodeJS之路径PATH模块
前面的话 path模块包含一系列处理和转换文件路径的工具集,通过 require('path') 可用来访问这个模块.本文将详细介绍path模块 路径组成 [path.dirname(p)] 返回路径 ...
- Python给多个变量赋值
# Assign values directly a, b = 0, 1 assert a == 0 assert b == 1 # Assign values from a list (r,g,b) ...
- form表单在前台转json对象
会发生序列化乱码问题,待解决. //根据表单id将其内空间,名称,值转为json var fireTraceEquipment =queryParamByFormId('form1'); functi ...
- oracle 体系结构简介
1.1.SGA(system global area) SGA是oracle Instance的基本组成部分,在示例启动是分配.是一组包含一个oracle实例的数据和控制信息的共享内存结构.主要用于存 ...
- debian安装dwm窗口管理器
我安装debian版本是debian-8.8.0-i386-netinst最小安装 首先去官网下载源代码 http://git.suckless.org/dwm #安装x-window环境 $sudo ...
- 13 用Css做下拉菜单
<style type="text/css"> * { margin: 0px; padding: 0px; font-family: &quo ...
- 卷积神经网络的变种: PCANet
前言:昨天和大家聊了聊卷积神经网络,今天给大家带来一篇论文:pca+cnn=pcanet.现在就让我带领大家来了解这篇文章吧. 论文:PCANet:A Simple Deep Learning Bas ...
- JS性能优化之怎么加载JS文件
IE8+等实行并行下载,各JS下载不受影响,但仍阻塞其他资源下载 如: 图片 所以首要规则就是:将JS放在body底部(推荐) 加载100kb的单个文件比4个25kb的文件快(减少外链文件数量)(脚本 ...
- phpcms V9 后台验证码图片不显示
某个网站在本地运行成功,上传到服务器上后,发现后台登陆的验证码图片不显示 根据网上提供的解决方案, 网站路径变量web_path没问题 database.system的配置路径没问题 apache的G ...
- maven下配置pom.xml
博主原创,转载请注明. 遇到的问题: 缺少依赖库.解决办法: 在build标签中添加: <plugin> <groupId>org.apache.maven.plugins&l ...