HDOJ 3829 Cat VS Dog
来源:互联网 发布:php 16进制转字符串 编辑:程序博客网 时间:2024/06/05 04:32
Cat VS DogTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)
Total Submission(s): 2561 Accepted Submission(s): 875
Total Submission(s): 2561 Accepted Submission(s): 875
Problem 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.
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)
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 2C1 D1D1 C11 2 4C1 D1C1 D1C1 D2D2 C1
Sample Output
13HintCase 2: Remove D1 and D2, that makes child 1, 2, 3 happy.
Source
2011 Multi-University Training Contest 1 - Host by HNU
题意:动物园里面只有猫和狗,对于每个人有喜欢的动物和不喜欢的动物,现在要删除一些动物,让最多的人满意,也就是最多的人喜欢的动物保留下来并且不喜欢的动物被删除。
直接来看结论,当达到最多的人的时候,留下来的人,没有矛盾,也就是没有人喜欢的动物是其他人不喜欢的动物,其余的人都是和这些人有矛盾的人,将这些人按照矛盾来划分为不同的集合,所有这里求的就是最大的集合,也就是图论中的最大独立集合,我们按照矛盾将这些人建边,如果两个人有矛盾,我们就在这两个人之间连边,最后求出最大的二分匹配,然后用人数剪出匹配的人数,剩下的就是答案。
#include<algorithm>#include<stdio.h>#include<queue>#include<string>#include<iostream>#include<string.h>#include<vector>#include<map>using namespace std;int use[5005];int r[5005];vector<int> a[5005];int n,m,p;bool match(int now){ int len=a[now].size(); for(int i=0;i<len;i++) { if(use[a[now][i]]==1) continue; use[a[now][i]]=1; if(r[a[now][i]]==-1||match(r[a[now][i]])) { r[a[now][i]]=now; return true; } } return false;}int solve(){ int ans=0; for(int i=1;i<=p;i++) { memset(use,0,sizeof(use)); //初始化 if(match(i)) ans++; } return ans;}int main(){ while(scanf("%d%d%d",&n,&m,&p)!=EOF) { for(int i=0;i<=5000;i++) a[i].clear(); memset(r,-1,sizeof(r)); map<int,string>like; map<int,string>notlike; string c1,c2; for(int i=1;i<=p;i++) { cin>>c1>>c2; like[i]=c1; notlike[i]=c2; } for(int i=1;i<p;i++) { for(int j=i+1;j<=p;j++) { if(like[i]==notlike[j]||notlike[i]==like[j]) { a[i].push_back(j); a[j].push_back(i); } } } int ans=solve(); printf("%d\n",p-ans/2); }return 0;}
0 0
- HDOJ 3829 Cat VS Dog
- HDU/HDOJ 3829 Cat VS Dog
- hdu 3829 Cat VS Dog
- HDU-3829 Cat VS Dog
- HDU 3829 CAT VS DOG
- HDU 3829 Cat VS Dog
- HDU 3829 Cat VS Dog
- HDU-3829 Cat VS Dog
- HDU 3829 Cat VS Dog
- 【HDU】 3829 Cat VS Dog
- HDU 3829 Cat VS Dog
- HDU 3829Cat VS Dog
- HDOJ题目3829Cat VS Dog(二分图,最大独立集)
- hdu 3829 Cat VS Dog 二分匹配
- Cat VS Dog
- Cat VS Dog
- Cat vs. Dog
- HDU2768 Cat vs. Dog
- 企业轻松组建虚拟局域网——蒲公英Cloud VPN技术
- class的装载
- tomcat 数据库连接池
- 外观模式(Facade)
- 少数人的智慧(The Wisdom of the Few)
- HDOJ 3829 Cat VS Dog
- 【JavaScript】性能更好的js动画实现方式——requestAnimationFrame
- 洛谷 P1080 国王游戏
- 2016最新号段验证(java)
- windows常用工具大汇集
- ubuntu14.04编译yael v438 遇到的一些问题
- SQL Server与Java的类型对应,Char用setString设值
- vs2008中连接ORACLE11G报错误:OCI error encountered. Error encountered while creating OCI environment.
- git 远程版本库,github提供服务原理,git自动更新发送邮件