(hdu step 6.3.7)Cat vs. Dog(当建边规则为:当一个观众和另外一个观众喜欢的东西产生冲突时建边,求最大独立集)

本文介绍了一档名为“Catvs.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.
* 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数字图像处理期末试卷复习资料](https://wenku.csdn.net/doc/771vi6wnkt?utm_source=wenku_answer2doc_content)HDU数字图像处理期末试卷复习资料》是一份宝贵的资源,它包含了历年试题重要知识点,能够帮助你针对性地准备期末考试。为了有效利用这份资料,你可以遵循以下步骤来制定一个复习计划: 1. 理解考试要评分标准:首先,明确考试的形式范围,包括理论部分实践操作部分,了解哪些内容是考试的重点,哪些部分需要特别注意。 2. 划分复习模块:根据数字图像处理课程的知识结构,将复习内容分为不同的模块,如图像处理基础理论、图像分析理解、实际应用案例、编程实现等。 3. 制定详细间表:为每个模块设定具体的复习间,合理分配间,确保每个部分都有足够的复习间。根据自己的掌握程度,对难以理解的部分可以适当增加复习间。 4. 理论知识复习:通过老师的讲义、课堂笔记以及复习资料中的概念性内容,全面复习理论知识。对于不熟悉或难以理解的理论,可以查找相关文献进行深入学习。 5. 重点题目练习:利用历年试题进行实战演练,重点关注计算题编程题,通过实际操作来加深对算法的理解。对于实践性题目,可以通过使用MATLAB、OpenCV等图像处理库进行实际编程练习。 6. 模拟测试:在复习计划的后期,进行模拟测试,按照正式考试的间限制来完成试题。这有助于你适应考试的节奏,同检验复习效果。 7. 错题集整理:对于模拟测试练习过程中出现的错误,要进行详细的整理复习,确保不再犯同样的错误。 8. 考前总结:考试前的最后几天,应该进行知识点的快速回顾,重点复习易错点重点难点,调整好心态,保持良好的作息,确保考试状态最佳。 利用《HDU数字图像处理期末试卷复习资料》进行有效复习,不仅能帮助你系统地掌握课程知识,还能提升你的实践操作能力,为考试做好充分准备。希望这份计划能够帮助你在期末考试中取得优异的成绩。 参考资源链接:[HDU数字图像处理期末试卷复习资料](https://wenku.csdn.net/doc/771vi6wnkt?utm_source=wenku_answer2doc_content)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值