Cat VS Dog

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)
Total Submission(s): 4751    Accepted Submission(s): 1757

Description:

The zoo have N cats and M dogs, today there are P children visiting the zoo, each child has a like-animal and a dislike-animal, if the child's like-animal is a cat, then his/hers dislike-animal must be a dog, and vice versa.
Now the zoo administrator is removing some animals, if one child's like-animal is not removed and his/hers dislike-animal is removed, he/she will be happy. So the administrator wants to know which animals he should remove to make maximum number of happy children.

Input:

The input file contains multiple test cases, for each case, the first line contains three integers N <= 100, M <= 100 and P <= 500.
Next P lines, each line contains a child's like-animal and dislike-animal, C for cat and D for dog. (See sample for details)

Output:

For each case, output a single integer: the maximum number of happy children.

Sample Input:

1 1 2
C1 D1
D1 C1
 
1 2 4
C1 D1
C1 D1
C1 D2
D2 C1

Sample Output:

1
3

Hint:

Case 2: Remove D1 and D2, that makes child 1, 2, 3 happy.
 
题意:
每个人都会喜欢一只猫(狗),相应的他会讨厌一只狗(猫),问现在要去掉多少猫or狗,可以让最多的人满意。
 
题解:
我一开始想的是对人和动物建图,让一个人快乐的话,就是留住一个动物以及去掉一个动物,但这样有点复杂,没做出来...
然后看了下其它的题解,发现如果一个人喜欢一只猫,另外一个人讨厌一只猫的话,那么就不可能让这两个人同时满意,最多只能让一个人满意。
这样问题就可以转化为最大独立集了,最大独立集的意思就是选取尽量多的点,并且这些点互不相邻。
想到这里建图就比较好建了,对人人建图。
 
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define mem(x) memset(x,0,sizeof(x))
using namespace std; const int N = ;
int n,m,p,ans;
char like[N][],dlike[N][]; //这里我一开始数组开的是4,WA了,开成5就AC了
int check[N],match[N],link[N][N]; inline int dfs(int x){
for(int i=;i<=p;i++){
if(link[x][i] && !check[i]){
check[i]=;
if(!match[i] || dfs(match[i])){
match[i]=x;
return ;
}
}
}
return ;
} int main(){
while(~scanf("%d%d%d",&n,&m,&p)){
mem(match);mem(link);ans=;
for(int i=;i<=p;i++){
scanf("%s%s",like[i],dlike[i]);
}
for(int i=;i<=p;i++)
for(int j=;j<=p;j++)
if(strcmp(like[i],dlike[j])== || strcmp(like[j],dlike[i])==) link[i][j]=;
for(int i=;i<=p;i++){
mem(check);
if(dfs(i)) ans++;
}
printf("%d\n",p-ans/);
}
return ;
}

HDU3829:Cat VS Dog(最大独立集)的更多相关文章

  1. HDU3829 Cat VS Dog —— 最大独立集

    题目链接:https://vjudge.net/problem/HDU-3829 Cat VS Dog Time Limit: 2000/1000 MS (Java/Others)    Memory ...

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

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

  3. HDU3829 Cat VS Dog

    题目链接:https://vjudge.net/problem/HDU-3829 题目大意: 有\(P\)个小孩,\(N\)只猫,\(M\)只狗.每个小孩都有自己喜欢的某一只宠物和讨厌的某一只宠物(其 ...

  4. HDU 3829 Cat VS Dog (最大独立集)【二分图匹配】

    <题目链接> 题目大意: 动物园有n条狗.m头猫.p个小孩,每一个小孩有一个喜欢的动物和讨厌的动物.如今动物园要转移一些动物.假设一个小孩喜欢的动物在,不喜欢的动物不在,他就会happy. ...

  5. hdu 2768 Cat vs. Dog 最大独立集 巧妙的建图

    题目分析: 一个人要不是爱狗讨厌猫的人,要不就是爱猫讨厌狗的人.一个人喜欢的动物如果离开,那么他也将离开.问最多留下多少人. 思路: 爱猫和爱狗的人是两个独立的集合.若两个人喜欢和讨厌的动物是一样的, ...

  6. (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) Tot ...

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

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

  8. Cat VS Dog HDU_3829(最大独立集最大匹配)

    Cat VS Dog 题意:一群小朋友去动物园,如果每个小朋友喜欢的动物是猫,那么不喜欢的动物一定是狗,反之也是.现在动物园的管理者要拿走一些动物,如果拿走的是某个小朋友不喜欢的动物,那这个小朋友就非 ...

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

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

随机推荐

  1. 003---Python基本数据类型--列表

    列表 .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px ...

  2. go学习笔记-常见命令

    常见命令 go 命令 可以在控制台执行go来查看 go Go is a tool for managing Go source code. Usage: go <command> [arg ...

  3. 20145202马超《JAVA》预备作业1

    20145202马超<JAVA>预备作业1 你觉得自己专业吗?对专业的期望是什么? 我觉得自己很不专业,我对专业的期望:老师之前讲过德国的一个研究,学习分为5个档次,第三个档是能够自己发现 ...

  4. 【python3.X】Scrapy学习途径参考

    如何爬取属性在不同页面的itemhttp://scrapy-chs.readthedocs.io/zh_CN/0.24/topics/request-response.html#topics-requ ...

  5. 13 ThreadLocal

    ThreadLocal 在多线程环境下,每个线程都有自己的数据.一个线程使用自己的局部变量比使用全局变量好,因为局部变量只有线程自己能看见,不会影响其他线程,而全局变量的修改必须加锁. 1. 使用函数 ...

  6. 1826: [JSOI2010]缓存交换

    1826: [JSOI2010]缓存交换 https://www.lydsy.com/JudgeOnline/problem.php?id=1826 分析: 简单的贪心,然后调啊调...最近怎么了,码 ...

  7. 自定义T4模板去掉实体对象中的下划线

            在EF Power Tool 默认使用的T4模板中,如果数据库表有下划线,那么生成的实体也有下划线,但是我们实际使用的过程中,是不希望有下划线的,要解决这个问题,可以自定义这个T4模板 ...

  8. error C2248: 'QObject::QObject' : cannot access private member declared in class 'QObject'

    1.error C2471: cannot update program database vc90.pdb 解决方案:https://blog.csdn.net/shuixin536/article ...

  9. vi/vim 命令使用详解

    1.Linux下创建文件 vi test.txt 或者 vim test.txt 或者 touch test.txt 2.vi/vim 使用 基本上 vi/vim 共分为三种模式,分别是命令模式(Co ...

  10. C++学习014函数值传递和地址传递

    当我们给一个函数传参数的时候,可以直接值传入函数,也给可以把一个地址传入函数 区别就是一个本身不被改变,而另一本身也在改变, 在开发时候都会用到, 这里做下记录 #include <iostre ...