HDU 3829 二分最大独立集
2016-04-12 17:02
399 查看
Cat VS Dog
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)Total Submission(s): 3391 Accepted Submission(s): 1185
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.
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 HintCase 2: Remove D1 and D2, that makes child 1, 2, 3 happy.
Source
2011 Multi-University Training Contest 1 - Host
by HNU
去掉某个孩子,使得开心的孩子最多,去掉的原则是那个孩子喜欢的动物是其他孩子不喜欢的,把他去掉,其他人就开心了
因为喜欢猫的必定不喜欢狗,喜欢狗的必定不喜欢猫,所以猫和猫,狗和狗之间不矛盾,所以可以建立二分图,左边为喜欢猫的人,右边为喜欢狗的人,如果,喜欢猫的人
不喜欢的那只狗是另外一个孩子喜欢的狗,那这两个孩子之间要连一根线,最后求的最大匹配数即为需要去掉的孩子的个数,然后再把总的孩子的个数去掉那个孩子即可
#include <iostream> #include <cstring> #define maxnum 555 using namespace std; int vis[maxnum]; int link[maxnum]; int head[maxnum]; int cnt; struct node { int v; int next; }list[maxnum*maxnum]; struct node2 { string cat; string dog; }catt[maxnum],dogg[maxnum]; void init() { cnt = 0; memset(head,-1,sizeof(head)); } void add(int u,int v) { list[cnt].v = v; list[cnt].next = head[u]; head[u] = cnt++; } int find(int u) { int i; for(i=head[u];~i;i=list[i].next) { int v = list[i].v; if(!vis[v]) { vis[v] = 1; if(link[v] == -1 || find(link[v])) { link[v] = u; return 1; } } } return 0; } int match(int N) { int i,sum=0; memset(link,-1,sizeof(link)); for(i=0;i<N;i++) { memset(vis,0,sizeof(vis)); if(find(i)) sum++; } return sum; } int main() { int i,j,k,kk; int a,b; int len; int u,v; int p,n,N,m; int flag; string s1,s2; int cat_num,dog_num; while(~scanf("%d%d%d",&n,&m,&p)) { init(); cat_num = dog_num = 0; for(i=1;i<=p;i++) { cin>>s1>>s2; if(s1[0] == 'C') { catt[cat_num].cat = s1; catt[cat_num++].dog = s2; } else { dogg[dog_num].cat = s1; dogg[dog_num++].dog = s2; } } for(i=0;i<cat_num;i++) { for(j=0;j<dog_num;j++) { if(catt[i].dog == dogg[j].cat || catt[i].cat == dogg[j].dog) add(i,j); } } printf("%d\n",p-match(cat_num)); } return 0; }
相关文章推荐
- Windows Dev Intro - Direct2D Compile Error
- Your project path contains non-ASCII characters(使用androidstudio常见错误)
- git基本使用
- netstat使用详解
- Android控件EditText的属性InputType的一些经验及动态设置TYPE_NUMBER_FLAG_DECIMAL无效的原因
- tomcat(6)生命周期
- zzulioj 1862: 我叫叶良辰 (Lucas推广)
- asp.net 点击服务器按钮 不触发后台事件解决方法
- JQuery实现提交(submit)表单时候验证所有文本框是否为空
- 小公司如何做项目管理?
- Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)
- 代理设计模式(一)静态代理
- Sql Server触发器案例(初学者学习案例)
- STM32 待机唤醒
- MemCache超详细解读
- hbase
- 计算函数运行时间
- ThinkPHP 3.2.3 验证码 生成和校验DEMO
- pcl里面使用KdTree来搜索
- java多线程系列:死锁及检测