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. write---向指定登录用户终端上发送信息

    write命令用于向指定登录用户终端上发送信息.通过write命令可传递信息给另一位登入系统的用户,当输入完毕后,键入EOF表示信息结束,write命令就会将信息传给对方.如果接收信息的用户不只登入本 ...

  2. uname---用于打印当前系统相关信息

    uname命令用于打印当前系统相关信息(内核版本号.硬件架构.主机名称和操作系统类型等). 语法 uname(选项) 选项 -a或--all:显示全部的信息: -m或--machine:显示电脑类型: ...

  3. UITextView自己定义键盘和系统键盘

    UITextView有inputView 和 inputAccessoryView 两个属性,都指定了对应的视图. inputAccessoryView 对象显示在 inputView 对象的上面.与 ...

  4. linux命令su与su-的差别

    su命令和su -命令最大的本质差别就是: su仅仅是切换了root身份.但Shell环境仍然是普通用户的Shell. 而su -连用户和Shell环境一起切换成root身份了. 仅仅有切换了Shel ...

  5. php数组增加元素

    php数组增加元素 截图 代码 <HTML> <HEAD> <TITLE>给数组增加元素</TITLE> </HEAD> <? $Ci ...

  6. windows “文件大小”与“占用空间”、文件系统与文件拷贝

    0. 文件大小与占用空间 "文件大小"和"占用空间"的差别 首先需要明确的是,"文件大小"代表着文件的真实大小(文件内容实际包含的全部字节数 ...

  7. 130.C++经典面试题 52-100

  8. installp 操作

    installp  软件安装和升级工具     1.查看某个已应用更可被提交或拒绝的文件集) installp -s   2. 应用更新TCP/IP软件( /usr/sys/inst.images ) ...

  9. Linux常用音乐播放器

    1.Rhythmbox是一个音乐播放和管理应用,GNOME桌面环境自带,它可以播放各种音频格式的音乐管理收藏的音乐.同时还具有音乐回放.音乐导入.刻录音频CD.显示专辑封面.显示歌词.DAAP共享等功 ...

  10. 如何在Centos官网下载所需版本的Centos——靠谱的Centos下载教程

    很多小伙伴不知道对应版本的Centos怎么下载,最近小编整理了一份Centos详细的下载教程,希望小伙伴们不在为下不到对应版本的Centos而苦恼. 1.进入Centos官网:https://www. ...