Cat VS Dog

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

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

Hint

Case 2: Remove D1 and D2, that makes child 1, 2, 3 happy.

 

Source

 
在有矛盾的男孩之间连边,有矛盾定义为两种情况:
  1.我喜欢的你不喜欢
  2.我不喜欢的你喜欢
建图后,求最大的两两互不相连的顶点集合即为答案,即求图的最大独立集。
一般图的最大独立集难求,转换为二分图,对男孩进行拆点,为i和p+i。若i和j有矛盾,则i与p+j、j与p+i连边。记得匹配数要除2。
 
定理:二分图最大独立集 == 顶点数 - 最小顶点覆盖 == 顶点数 - 二分图最大匹配
  1. //2017-08-25
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. const int N = ;
  10. const int M = ;
  11. int head[N], tot;
  12. struct Edge{
  13. int to, next;
  14. }edge[M];
  15.  
  16. void init(){
  17. tot = ;
  18. memset(head, -, sizeof(head));
  19. }
  20.  
  21. void add_edge(int u, int v){
  22. edge[tot].to = v;
  23. edge[tot].next = head[u];
  24. head[u] = tot++;
  25.  
  26. edge[tot].to = u;
  27. edge[tot].next = head[v];
  28. head[v] = tot++;
  29. }
  30.  
  31. int n, m, p;
  32. string G[N];
  33. int matching[N];
  34. int check[N];
  35.  
  36. bool dfs(int u){
  37. for(int i = head[u]; i != -; i = edge[i].next){
  38. int v = edge[i].to;
  39. if(!check[v]){//要求不在交替路
  40. check[v] = ;//放入交替路
  41. if(matching[v] == - || dfs(matching[v])){
  42. //如果是未匹配点,说明交替路为增广路,则交换路径,并返回成功
  43. matching[u] = v;
  44. matching[v] = u;
  45. return true;
  46. }
  47. }
  48. }
  49. return false;//不存在增广路
  50. }
  51.  
  52. //hungarian: 二分图最大匹配匈牙利算法
  53. //input: null
  54. //output: ans 最大匹配数
  55. int hungarian(){
  56. int ans = ;
  57. memset(matching, -, sizeof(matching));
  58. for(int u = ; u <= p; u++){
  59. if(matching[u] == -){
  60. memset(check, , sizeof(check));
  61. if(dfs(u))
  62. ans++;
  63. }
  64. }
  65. return ans;
  66. }
  67.  
  68. string like[N], dislike[N];
  69.  
  70. int main()
  71. {
  72. std::ios::sync_with_stdio(false);
  73. //freopen("inputJ.txt", "r", stdin);
  74. while(cin>>n>>m>>p && n){
  75. init();
  76. for(int i = ; i <= p; i++)
  77. cin>>like[i]>>dislike[i];
  78. for(int i = ; i <= p; i++){
  79. for(int j = ; j < i; j++){
  80. if(like[i] == dislike[j] || dislike[i] == like[j]){
  81. add_edge(i, p+j);
  82. add_edge(j, p+i);
  83. }
  84. }
  85. }
  86. cout<<p-hungarian()/<<endl;
  87. }
  88.  
  89. return ;
  90. }

HDU3829(KB10-J 二分图最大独立集)的更多相关文章

  1. HDU 3829 - Cat VS Dog (二分图最大独立集)

    题意:动物园有n只猫和m条狗,现在有p个小孩,他们有的喜欢猫,有的喜欢狗,其中喜欢猫的一定不喜欢狗,喜欢狗的一定不喜欢猫.现在管理员要从动物园中移除一些动物,如果一个小孩喜欢的动物留了下来而不喜欢的动 ...

  2. BZOJ3175:[TJOI2013]攻击装置(二分图最大独立集)

    Description 给定一个01矩阵,其中你可以在0的位置放置攻击装置.每一个攻击装置(x,y)都可以按照“日”字攻击其周围的 8个位置(x-1,y-2),(x-2,y-1),(x+1,y-2), ...

  3. [luoguP3355] 骑士共存问题(二分图最大独立集)

    传送门 模型 二分图最大独立集,转化为二分图最大匹配,从而用最大流解决. 实现 首先把棋盘黑白染色,使相邻格子颜色不同. 把所有可用的黑色格子看做二分图X集合中顶点,可用的白色格子看做Y集合顶点. 建 ...

  4. 洛谷 - P3033 - 牛的障碍Cow Steeplechase - 二分图最大独立集

    https://www.luogu.org/fe/problem/P3033 二分图最大独立集 注意输入的时候控制x1,y1,x2,y2的相对大小. #include<bits/stdc++.h ...

  5. 洛谷 - P5030 - 长脖子鹿放置 - 二分图最大独立集

    https://www.luogu.org/problemnew/show/P5030 写的第一道黑色题,图建对了. 隐约觉得互相攻击要连边,规定从奇数行流向偶数行. 二分图最大独立集=二分图顶点总数 ...

  6. 【Codevs1922】骑士共存问题(最小割,二分图最大独立集转最大匹配)

    题意: 在一个n*n个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示.棋盘上某些方格设置了障碍,骑士不得进入. 对于给定的n*n个方格的国际象棋棋盘和障碍标志,计算棋盘上最多可以放置多少个 ...

  7. UVA-12083 Guardian of Decency 二分图 最大独立集

    题目链接:https://cn.vjudge.net/problem/UVA-12083 题意 学校组织去郊游,选择最多人数,使得任意两个人之间不能谈恋爱 不恋爱条件是高差大于40.同性.喜欢的音乐风 ...

  8. TopCoder12808 「SRM594Medium」FoxAndGo3 二分图最大独立集

    问题描述 一个 \(N \times N\) 围棋棋盘,任意两个白子不相邻,你要加入若干个黑子并提出白子,最大化空格数目. submit 题解 显然最终棋盘的局面不能够一个白子和它周围的空格都是空的, ...

  9. 长脖子鹿放置【洛谷P5030】二分图最大独立集变形题

    题目背景 众周所知,在西洋棋中,我们有城堡.骑士.皇后.主教和长脖子鹿. 题目描述 如图所示,西洋棋的“长脖子鹿”,类似于中国象棋的马,但按照“目”字攻击,且没有中国象棋“别马腿”的规则.(因为长脖子 ...

随机推荐

  1. 19_python_反射

    一.内置函数(补充)          1.issubclass() -- 方法用于判断参数 class 是否是类型参数 classinfo 的子类.   语法格式:issubclass(class, ...

  2. 全世界最顶级黑客同时沸腾在DEF CON 25,是怎样一种体验?

    2017,我在这里!圆你黑客梦!!   被称为黑客“世界杯”与“奥斯卡”的美国黑帽技术大会Black Hat和世界黑客大会DEF CON 是众多黑客心中最神圣的梦!   有位小表弟告诉我说:Black ...

  3. JS获取浏览器URL中查询字符串的参数

    首先要知道Location这个对象以及这个对象中的一些属性: href:设置或返回完整的url.如本博客首页返回http://www.cnblogs.com/wymninja/ host:设置或返回主 ...

  4. MySQL 逻辑物理备份测试

    目录 逻辑备份 mysqldump 普通备份 mysqlpump 并行备份 mysqlpump 压缩并行备份 mydumper 并行备份 mydumper 并行压缩备份 小结 物理备份 xtrabac ...

  5. 微信小程序导出当前画布指定区域的内容并生成图片保存到本地相册(canvas)

    最近在学小程序,在把当前画布指定区域的内容导出并生成图片保存到本地这个知识点上踩坑了. 这里用到的方法是: wx.canvasToTempFilePath(),该方法作用是把当前画布指定区域的内容导出 ...

  6. jdk1.8+SpringAOP注解报java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut select错误的不知原因的解决办法[仅供参考]

    先说办法:如果Aspectweaver-1.*.*jar这三个包版本比较低, 比如1.5.0这一层次的,可以找版本高一点的包替换低版本的包,问题可以得到解决 jar包的下载地址:https://mvn ...

  7. hdu5745--La Vie en rose (DP+bitset)

    好题,学到新姿势! 题意:给两个字符串 a 和 b ,b可以进行变换,规则是可以任意交换相邻两个字符的位置,但是不可以有交叉(例如3和4交换,5和6交换 互不影响,但是2和3,3和4就不可以).求a中 ...

  8. (转)linux用户态和内核态理解

    原文:https://blog.csdn.net/buptapple/article/details/21454167 Linux探秘之用户态与内核态-----------https://www.cn ...

  9. Executor框架(一)

    类继承关系 更详细的继承关系: ExecutorComplitionService类 在说Executor接口及实现类之前,先聊聊ExecutorComplitionService. 成员变量 pri ...

  10. SQL Server 2016 需要单独安装 SSMS

    默认安装完 MSSQL 后,不自带 SSMS 的管理工具了,需要的话可以单独安装,貌似更专业了一些. https://msdn.microsoft.com/library/mt238290.aspx ...