Cat vs. Dog

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1520 Accepted Submission(s): 570

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
解题:如今是要求满足最多人数的要求,从正面建图有困难。那么反建法,图的每一条边都是不可取的情况。而我们要求的是可取的情况,所以就变成了求最大独立集问题。
首先把v个人分成两个集合。一个集合是爱猫,还有一个是爱狗。这样每一个集合内部没有冲突。然后用反建法建图,cat[i].love==dog[j].hate 或 cat[i].heat==dog[j].love.建边。建完后就按常规求出了。
#include<stdio.h>
#include<string.h>
struct nn
{
char love[5];
char hate[5];
}cat[505],dog[505];
int map[505][505],vist[505],match[505],dog_n;
int find(int i)
{
for(int j=1;j<=dog_n;j++)
if(!vist[j]&&map[i][j])
{
vist[j]=1;
if(match[j]==0||find(match[j]))
{
match[j]=i; return 1;
}
}
return 0;
}
int main()
{
int t,n,m,v,cat_n;
char ch1[5],ch2[5];
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&v);
cat_n=0; dog_n=0;
for(int i=1;i<=v;i++)
{
scanf("%s%s",ch1,ch2);
if(ch1[0]=='C')
{
strcpy(cat[++cat_n].love,ch1);
strcpy(cat[cat_n].hate,ch2);
}
else
{
strcpy(dog[++dog_n].love,ch1);
strcpy(dog[dog_n].hate,ch2);
}
}
memset(map,0,sizeof(map));
for(int i=1;i<=cat_n;i++)
for(int j=1;j<=dog_n;j++)
if(strcmp(cat[i].love,dog[j].hate)==0||strcmp(cat[i].hate,dog[j].love)==0)
map[i][j]=1;
memset(match,0,sizeof(match));
int ans=0;
for(int i=1;i<=cat_n;i++)
{
memset(vist,0,sizeof(vist));
ans+=find(i);
}
printf("%d\n",v-ans);
}
}

hdu2768Cat vs. Dog (反建法,最大独立集)的更多相关文章

  1. hdu2768-Cat vs. Dog:图论:二分匹配

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

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

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

  3. 线段树(结构体建法_QAQ)

    线段树(结构体)模板 #include<iostream> #include<cstdio> #include<queue> #include<cstring ...

  4. hdu-2768-Cat vs. Dog(二分图-最大匹配数)

    题意: 有猫C个和狗D个,有V个投票人,每个人喜欢猫讨厌狗或则喜欢狗讨厌猫! 求最多能满足多少投票人. 分析: 两个投票者矛盾的话就连一条边,总数减去最大匹配数/2就是要求的答案 // File Na ...

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

    题意:有P个孩子,有的孩子喜欢猫不喜欢狗,有的喜欢狗不喜欢猫(喜欢的和不喜欢的一定是相相对立的动物),动物园有N只猫,M只狗,每个孩子都有喜欢的猫讨厌的狗(或者喜欢的狗讨厌的猫),现在动物园要送走一批 ...

  6. ACM-由数据范围反推算法复杂度以及算法内容

    一般ACM或者笔试题的时间限制是1秒或2秒. 在这种情况下,C++代码中的操作次数控制在 \(10^7\) 为最佳. 下面给出在不同数据范围下,代码的时间复杂度和算法该如何选择: 数据范围 算法选择 ...

  7. HDU 4725 The Shortest Path in Nya Graph(spfa+虚拟点建图)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题目大意:有n层,n个点分布在这些层上,相邻层的点是可以联通的且距离为c,还有额外给出了m个条边 ...

  8. 10月清北学堂培训 Day 3

    今天是钟皓曦老师的讲授~ zhx:题很简单,就是恶心一些qwq~ T1 别人只删去一个字符都能AC,我双哈希+并查集只有40?我太菜了啊qwq 考虑到越短的字符串越难压缩,越长的字符串越好压缩,所以我 ...

  9. 个人训练记录(UPD 9.16)

    本文章记录一些较难的题,摘自自己的blog中的其他文章.也有些单独成章有点浪费的题也写在里面了. 2019.7.15-2019.7.21 1182F(2900) 题意:求在区间 \([a,b]\) 中 ...

随机推荐

  1. 一次Linux LVM VG丢失完整找回过程记录

    某客户的一台PC服务器连接了一台HP EVA 的FC SAN存储,划了一个6T的LUN分作一个单独的VG使用,在某一次异常掉电之后,发现该VG完全丢失,使用vgs/pvs/lvs命令均无法找到此VG及 ...

  2. zookeeper 性能测试

    zookeeper压力测试:性能对比(3个节点,5个节点,7个节点 创建节点.删除节点.设置节点数据.读取节点数据性能及并发性能) 测试结果如下: 五次测试三节点结果: 创建100W节点用时:15.0 ...

  3. 移动端web app开发备忘

    近期要做个手机html5的页面,做些知识储备,重要的点记录下来以备兴许. 1.devicePixelRatio:定义设备物理象素和设备独立象素的比例.css中的px能够看作是设备的独立象素.通过dev ...

  4. BestCoder Round #11 (Div. 2)

    太菜,仅仅能去Div2.(都做不完 ORZ... 各自是 HDU: 5054pid=5054"> Alice and Bob 5055Bob and math problem 5056 ...

  5. SpringBoot结合MongoDB入门

    MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系 ...

  6. centos7 zabbix3.4.6显示中文乱码问题

    工具 : winscp (Linux Windows 传输文件工具) 当部署完zabbix然后显示中文会出现如下图 然后此时先去windows的文字目录如 C:\Windows\Fonts 随便托个字 ...

  7. 22.dll调用技术

    1.dll文件: #include <Windows.h> _declspec(dllexport) void message_hello() { MessageBoxA(, ); } _ ...

  8. 消息推送学习一、原生Socket的使用

    消息推送也是客户端和服务器连接然后进行交互的一种形式,但是不同于HTTP的连接,这种连接需要长时间的进行,当有消息时可以及时推送到客户端.除此之外还有多个用户,可能需要针对其身份进行不同的推送等等要求 ...

  9. 动态调用web服务 --WSHelper.cs

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Net;us ...

  10. 技嘉H81M-DS2 主板安装 XP方法,及网卡驱动安装

    这是微软联合厂家封杀XP的结果,目的很简单,微软只想把你驱赶到WIN7.WIN8上去. 16.7.18 技嘉H81M-S1, G3260 安装XP系统 *BIOS 修改 Storage Boot Op ...