称号:

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.
* v lines with two pet identifiers each. The first is the pet that this voter wants to keep, the second is the pet that this voter wants to throw out. A pet identifier starts with one of the characters `C' or `D', indicating whether the pet is a cat or dog, respectively. The remaining part of the identifier is an integer giving the number of the pet (between 1 and c for cats, and between 1 and d for dogs). So for instance, ``D42'' indicates dog number 42.

 
Output
Per testcase:

* One line with the maximum possible number of satisfied voters for the show.

 
Sample Input
2
1 1 2
C1 D1
D1 C1
1 2 4
C1 D1
C1 D1
C1 D2
D2 C1
 
Sample Output
1
3
 
 
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(当施工方规则:建边当观众和其他观众最喜爱的东西冲突,求最大独立集)的更多相关文章

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

  2. HDU 3829——Cat VS Dog——————【最大独立集】

    Cat VS Dog Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit S ...

  3. HDU 3289 Cat VS Dog (二分匹配 求 最大独立集)

    题意:每个人有喜欢的猫和不喜欢的狗.留下他喜欢的猫他就高心,否则不高心.问最后最多有几个人高心. 思路:二分图求最大匹配 #include<cstdio> #include<cstr ...

  4. hdu 3829 Cat VS Dog 二分图匹配 最大点独立集

    Cat VS Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Prob ...

  5. Cat VS Dog HDU - 3829 (最大独立集 )

    Cat VS Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)Total ...

  6. hdu 2768 Cat vs. Dog (二分匹配)

    Cat vs. Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. HDU——2768 Cat vs. Dog

    Cat vs. Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  8. Cat VS Dog

    Cat VS Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)Total ...

  9. Hdu3829 Cat VS Dog(最大独立点集)

    Cat VS Dog Problem Description The zoo have N cats and M dogs, today there are P children visiting t ...

随机推荐

  1. HDOJ 5276 YJC tricks time multimap

    multimap的使用 YJC tricks time Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/262144 K ...

  2. 用标准Struts2+mvc写的用户管理

    这星期的实验,最终调好了. 一句话,麻雀虽小,五脏俱全.相信刚学struts2的同学能够通过该实验能够更好地理解struts的结构和mvc 登录的之前写过,这里直接进入用户管理 用struts2都要在 ...

  3. windows phone 7 客户端和web的交互(WebBrowser的使用)

    原文:windows phone 7 客户端和web的交互(WebBrowser的使用) 前几天看到淘宝的Android客户端,有种促销的功能,当点击促销的时候连接的淘宝促销wap页面,然后点击商品后 ...

  4. Oracle安装过程物理内存检查及临时temp空间不足解决办法

    物理内存 – 此先决条件将测试系统物理内存总量是否至少为 922MB (944128.0KB). 预期值 : N/A 实际值 : N/A 错误列表: – 可用物理内存 PRVF-7531 : 无法在节 ...

  5. [Cocos2d-x v3.x]浅谈容器Vector

    转载请注明来自:star特530的CSDN博客 http://blog.csdn.net/start530/article/details/19170853 前两天有人问我说在3.0 beta2版本号 ...

  6. CentOS6.5 Nginx优化编译配置[续]

    继续上文CentOS6.5 Nginx优化编译配置本文记录有关Nginx系统环境的一些细节设置,有关Nginx性能调整除了配置文件吻合服务器硬件之前就是关闭不必要的服务.磁盘操作.文件描述符.内核调整 ...

  7. jQuery来源学习笔记:扩展的实用功能

    // 扩展的实用功能 jQuery.extend({ // http://www.w3school.com.cn/jquery/core_noconflict.asp // 释放$的 jQuery 控 ...

  8. Error 56: The Cisco Systems, Inc. VPN Service has not been started(Cisco VPN在Vista下出现Error 56的解决办法)

    Error 56: The Cisco Systems, Inc. VPN Service has not been started(Cisco VPN在Vista下出现Error 56的解决办法) ...

  9. android下调试声卡驱动之概述

    在Android中音频系统使用的是ALSA系统架构.ASoC--ALSA System on Chip .是建立在标准ALSA驱动层上,为了更好地支持 嵌入式处理器和移动设备中的音频Codec的一套软 ...

  10. SQL声明大全

    1.随机选择3记录     select top 3 * from tablename newid() 2.随机选记录     select newid(). 3.删除反复记录 1) delete f ...