HDU 3829 - Cat VS Dog【二分图最大匹配最大独立集】
2016-03-12 15:01
477 查看
SubmitStatus
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
瞅着眼熟,和暑假做过的
题意说如果小孩喜欢的动物没拿掉,不喜欢的拿掉了,那么happy值++。我们换个想法,如果存在了两个小孩的喜恶相反,才会出现只能满足一方要求的情况,我们用小孩的喜恶来建边,而不要纠结于猫还是狗,矛盾了就建边。最大独立集=点数-最大匹配
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.
瞅着眼熟,和暑假做过的
HDU 2768Cat vs. Dog
除了叙述不一样,其他都一样,之前纠结于如果是把猫放左边,狗放右边,那喜欢狗不喜欢猫的怎么办?灵光乍一闪想到之前的题。题意说如果小孩喜欢的动物没拿掉,不喜欢的拿掉了,那么happy值++。我们换个想法,如果存在了两个小孩的喜恶相反,才会出现只能满足一方要求的情况,我们用小孩的喜恶来建边,而不要纠结于猫还是狗,矛盾了就建边。最大独立集=点数-最大匹配
#include <iostream> #include <stdio.h> #include <string.h> #define M 550 #define inf 0x3f3f3f3f using namespace std; const int MAXN=550; int uN,vN;//u,v数目 int g[MAXN][MAXN]; int linker[MAXN]; bool used[MAXN]; bool dfs(int u)//从左边开始找增广路径 { int v; for(v=0;v<vN;v++)//这个顶点编号从0开始,若要从1开始需要修改 if(g[u][v]&&!used[v]) { used[v]=true; if(linker[v]==-1||dfs(linker[v])) {//找增广路,反向 linker[v]=u; return true; } } return false;//这个不要忘了,经常忘记这句 } int hungary() { int res=0; int u; memset(linker,-1,sizeof(linker)); for(u=0;u<uN;u++) { memset(used,0,sizeof(used)); if(dfs(u)) res++; } return res; } char like[550][10],dislike[550][10]; int main() { // freopen("cin.txt","r",stdin); int c,d,v; while(~scanf("%d%d%d",&c,&d,&v)) { vN=uN=v; for(int i=0;i<v;i++) scanf("%s%s",like[i],dislike[i]); memset(g,0,sizeof(g)); for(int i=0;i<v;i++) for(int j=0;j<v;j++) { if(strcmp(like[i],dislike[j])==0||strcmp(like[j],dislike[i])==0) g[i][j]=1; } printf("%d\n",v-hungary()/2); } return 0; }
相关文章推荐
- Google Java 编码规范
- jQuery对象与DOM对象之间的转换
- java设计模式之前戏
- MVC下的cshtml和aspx页面
- 最短路spfa POJ 1502 MPI Maelstrom
- Codeforce 567C(Geometric Progression) 奇淫巧计...
- Git和SVN之间的五个基本区别
- 深入理解iOS开发中的BitCode功能
- Java对象排序
- android 定时器的使用
- 软件工程第一次作业
- 测试新人如何提高自己
- springmvc上传文件出现的一些状况
- Android学习之 Intent显示和Intent隐式
- Android学习之 错误信息显示
- “问吧”调查问卷的分析总结与感受
- leetcode Reverse Nodes in k-Group
- js-DOM给class追加一个新的元素
- 剑指offer-构建乘积数组
- HDU 2156 分数矩阵 (水题)