(hdu step 6.3.7)Cat vs. Dog(当施工方规则:建边当观众和其他观众最喜爱的东西冲突,求最大独立集)
称号:
Cat vs. Dog |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission(s): 219 Accepted Submission(s): 86 |
Problem Description
The latest reality show has hit the TV: ``Cat vs. Dog''. In this show, a bunch of cats and dogs compete for the very prestigious Best Pet Ever title. In each episode, the cats and dogs get to show themselves off, after which the viewers vote on which pets should stay and which should be forced to leave the show.
Each viewer gets to cast a vote on two things: one pet which should be kept on the show, and one pet which should be thrown out. Also, based on the universal fact that everyone is either a cat lover (i.e. a dog hater) or a dog lover (i.e. a cat hater), it has been decided that each vote must name exactly one cat and exactly one dog. Ingenious as they are, the producers have decided to use an advancement procedure which guarantees that as many viewers as possible will continue watching the show: the pets that get to stay will be chosen so as to maximize the number of viewers who get both their opinions satisfied. Write a program to calculate this maximum number of viewers. |
Input
On the first line one positive number: the number of testcases, at most 100. After that per testcase:
* One line with three integers c, d, v (1 ≤ c, d ≤ 100 and 0 ≤ v ≤ 500): the number of cats, dogs, and voters. |
Output
Per testcase:
* One line with the maximum possible number of satisfied voters for the show. |
Sample Input
2 |
Sample Output
1 |
Source
NWERC 2008
|
Recommend
lcy
|
题目大意:
有v个观众,分别投出给自己喜欢的动物和讨厌的动物。
假设一个观众喜欢的动物和另外一个观众喜欢的动物发生冲突,则让一个观众离开,问最后可以留下几个观众。
、
例子分析:
输入分析:
2
1 1 2
C1 D1//第一个观众喜欢的是Cat1 讨厌的是Dog1
D1 C1
1 2 4
C1 D1
C1 D1
C1 D2
D2 C1
输出分析:
在第二个例子中。输出结果3是怎么得到的呢?我们把喜欢猫的放在一边喜欢狗的放在一边。然后依据冲突的产生情况建边,这时候我们就能得到下图:
这时候最大匹配就是1----4, 2----4 , 3-----4 这三条边中的一条。上图选择了1----4这条边来举例。假设还想不明确的同学,在这里我们复习一下相关概念
匹配:是一个边的集合,随意两条边不存在公共点。
最大匹配:变数最多的匹配。
题目分析:
把喜欢猫的放在一边,把喜欢狗的放在一边,若发生了冲突。则建一条边。则问题转化成二分图的最大独立集问题。
须要注意一下的是这道题中的建边规则。
这道题使用邻接矩阵实现的。耗时好像是390ms。用邻接表可能会快一些。
代码例如以下:
/*
* g.cpp
*
* Created on: 2015年3月14日
* Author: Administrator
*/ #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; const int maxn = 501; int map[maxn][maxn];
bool useif[maxn];
int link[maxn]; int n; bool can(int t){
int i;
for(i = 1 ; i <= n ; ++i){
if(useif[i] == false && map[t][i] == true){
useif[i] = true;
if(link[i] == -1 || can(link[i])){
link[i] = t; return true;
}
}
} return false;
} int max_match(){
int num = 0; int i;
for(i = 1 ; i <= n ; ++i){
memset(useif,false,sizeof(useif));
if(can(i) == true){
num++;
}
} return num;
} int main(){
string loves[maxn];
string hates[maxn]; int t;
scanf("%d",&t);
while(t--){
memset(map,false,sizeof(map));
memset(link,-1,sizeof(link)); int cats,dogs;
scanf("%d%d%d",&cats,&dogs,&n); int i;
for(i = 1 ; i <= n ; ++i){
cin >> loves[i] >> hates[i];
} /**
* 这道题与其它题不同的地方就在于建边部分.
* 所以其它部分不再加凝视,在这里仅仅为建边过程加凝视
*/
int j;
for(i = 1 ; i <= n ; ++i){//遍历每个观众的喜欢的动物
for(j = 1 ; j <= n ; ++j){//遍历每个观众讨厌的动物
//假设第i个观众喜欢的动物==第j个观众讨厌的动物 || 第i个观众讨厌的动物 == 第j个观众喜欢的动物。 证明i和j之间存在冲突
if(loves[i] == hates[j] || hates[i] == loves[j]){
//为i和j建边
map[i][j] = true;
map[j][i] = true;
}
}
} printf("%d\n",n - max_match()/2);
} return 0;
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
(hdu step 6.3.7)Cat vs. Dog(当施工方规则:建边当观众和其他观众最喜爱的东西冲突,求最大独立集)的更多相关文章
- 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, toda ...
- HDU 3829——Cat VS Dog——————【最大独立集】
Cat VS Dog Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit S ...
- HDU 3289 Cat VS Dog (二分匹配 求 最大独立集)
题意:每个人有喜欢的猫和不喜欢的狗.留下他喜欢的猫他就高心,否则不高心.问最后最多有几个人高心. 思路:二分图求最大匹配 #include<cstdio> #include<cstr ...
- hdu 3829 Cat VS Dog 二分图匹配 最大点独立集
Cat VS Dog Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others) Prob ...
- Cat VS Dog HDU - 3829 (最大独立集 )
Cat VS Dog Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)Total ...
- hdu 2768 Cat vs. Dog (二分匹配)
Cat vs. Dog Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU——2768 Cat vs. Dog
Cat vs. Dog Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- Cat VS Dog
Cat VS Dog Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)Total ...
- Hdu3829 Cat VS Dog(最大独立点集)
Cat VS Dog Problem Description The zoo have N cats and M dogs, today there are P children visiting t ...
随机推荐
- Set vs. Set<?>(转)
You may know that an unbounded wildcard Set<?> can hold elements of any type, and a raw type S ...
- 寻找失踪的整数数组(Find the missing integer)
排列a包含N分子,其元素属于[0,N]之间,且不存在反复的元素.请你找出数组中缺失的元素(由于[0,N]之间有N+1个元素.而数组仅仅能存储N个元素.所以必定缺少一个元素).当中对数组的操作满足下列的 ...
- Windows Phone开发(21):做一个简单的绘图板
原文:Windows Phone开发(21):做一个简单的绘图板 其实我们今天要说的就是一个控件--InkPresenter,这个控件并不是十分强大,没办法和WPF中的InkCanvas相比,估计在实 ...
- 优化数据页面(18)——标注keyword
优化数据页面(18)--标注keyword 设计要点:优化数据页面.界面设计.美化exce 秀秀:事实上俺认为,相同是数据项,它们的重要程度也不同. 阿金:嗯? 秀秀:每一行数据时描写叙述一条信息的, ...
- Storm On YARN带来的好处
1)弹性计算资源 将storm执行在yarn上后.Storm能够与其它计算框架(如mapreduce)共享整个集群的资源.这样当Storm负载骤增时,可动态为它添加计算资源. 负载减小时,能够 ...
- 通过openssh远程登录时的延迟问题解决
Linux下的ssh 服务器一般用的都是open-ssh,可是发现有些时候通过ssh连接服务器时总会有大概10秒钟左右的延迟. 一开始以为是openssh的安全策略,防止端口扫描,后来发现自己想多了. ...
- JS伪3D 图形透视效果
本文地址:http://blog.csdn.net/ei__nino/article/details/9243331 本来是想实现多个圆片的透视效果,对于运算都是测试得出的.不是严谨的数学计算. 使用 ...
- Apache Rewrite 理解
因为工作须要,查了一下Apache的文档,对当中反向引用和条件的运行做了理解和实验,以下是对Apache 2.2文档的摘录,并在上面做了实验的样例说明,希望能给一些须要深入理解的一些帮助. 其它部分就 ...
- Android KitKat 4.4 Wifi移植AP模式和网络共享的调试日志
Tethering技术在移动平台上已经运用的越来越广泛了.它能够把移动设备当做一个接入点,其它的设备能够通过Wi-Fi.USB或是Bluetooth等方式连接到此移动设备.在Android中能够将Wi ...
- ajax j跨域请求sonp
需求 遇到的问题 解决方案 需求 如今,该项目需要获得数据访问外部链接.它是跨域.使用ajax 直提示: 遇到的问题 1. 怎样使用ajax 跨域请求数据 2. 能不能post请求 解决的方法 经过网 ...