题目链接:https://vjudge.net/problem/HDU-3829

Cat VS Dog

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

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
 
Recommend
xubiao

题解:

1.如果小孩u喜欢的与小孩v讨厌的相同,或者小孩u讨厌的与小孩v喜欢的相同,则表明他们两个人有冲突。在u和v之间连一条边。

2.利用匈牙利算法,求出最大匹配数cnt,即为最小点覆盖。答案就为 n - cnt 。为何?

答:所谓最小点覆盖,即用最少的点,去覆盖掉所有的边。如果我们把这最小覆盖点集都删除,那么图中就不存在边了,也就是不存在冲突,剩下的人可以和平共处了。又因为是“最小”点覆盖, 所以删除的点是最少的,所以留下来的点是最多的。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
const int INF = 2e9;
const int MOD = 1e9+;
const int MAXN = +; int n, m, p;
int M[MAXN][MAXN], link[MAXN];
bool vis[MAXN];
char like[MAXN][], hate[MAXN][]; bool dfs(int u)
{
for(int i = ; i<=p; i++)
if(M[u][i] && !vis[i])
{
vis[i] = true;
if(link[i]==- || dfs(link[i]))
{
link[i] = u;
return true;
}
}
return false;
} int hungary()
{
int ret = ;
memset(link, -, sizeof(link));
for(int i = ; i<=p; i++)
{
memset(vis, , sizeof(vis));
if(dfs(i)) ret++;
}
return ret;
} int main()
{
while(scanf("%d%d%d", &n, &m, &p)!=EOF)
{
for(int i = ; i<=p; i++)
scanf("%s%s", like[i], hate[i]); memset(M, false, sizeof(M));
for(int i = ; i<=p; i++)
for(int j = ; j<=p; j++)
if(!strcmp(like[i], hate[j]) || !strcmp(hate[i], like[j]))
M[i][j] = true; int cnt = hungary()/;
printf("%d\n", p-cnt);
}
}

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

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

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

  2. HDU3829 Cat VS Dog

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

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

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

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

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

  5. HDU3829:Cat VS Dog(最大独立集)

    Cat VS Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)Total ...

  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. 404 Not Found 由来

    404 NOT FOUND! 抱歉,沒有找到您需要的文章!! 什么是 404 Not Found 404页面是网站必备的一个页面,它承载着用户体验与SEO优化的重任.404页面通常为用户访问了网站上不 ...

  2. 安装weblogic时,运行configure.cmd报错、闪退、无法创建域

    直接运行configure.cmd时在jar包加载完成时,不提示创建域的过程,而是直接退出程序 命令行: cd /d F:\00uep_rfs\wls1212_dev\wls12120 切换至解压路径 ...

  3. Halloween Costumes(区间DP)

    Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he is planning t ...

  4. [luoguP1437] [HNOI2004]敲砖块(DP)

    传送门 可以得到一个性质,如果打掉第i列的第j个,那么第i列的1~j-1个也会打掉. 如果第i列打j个,那么第i+1列至少打j-1个. #include <cstdio> #include ...

  5. hdu 4790 数学

    /* 题意:给你二个区间[a,b]和[c,d] 分别从中选一个数x和y使的(x+y)%p=m; 可以这样来求,先求出(0->b和0->d区间段的值)-(区间0->a-1和0-> ...

  6. FLEX中restrict限定TextInput输入

    restrict限制的意思 1. 限制某个字符的输入,用符号 ^ 跟上要限制的字符,可跟多个字符  <!-- 限制字符"~"的输入 --> <mx:TextInp ...

  7. Flex里监听mouseDownOutside事件解决弹出窗口点击空白关闭功能

    其实当用户在使用 PopUpManager 打开的某个组件外部单击时,会从该组件分派一个mouseDownOutside事件 监听该事件就能实现点击空白处关闭窗口的功能 this.addEventLi ...

  8. iOS FMDB 无法更新二进制数据的问题

    使用FMDB很方便的实现了(通过数据库字段名而不是字段索引)数据的读取,插入,更新,删除.但是我在更新图片时发现通过格式化字符(@“%@”,data/NSData/)传入的二进制数据更新到数据库后不能 ...

  9. (转)Redis

    Rdis和JQuery一样是纯粹为应用而产生的,这里记录的是在CentOS 5.7上学习入门文章: 1.Redis简介 Redis是 一个key-value存储系统.和Memcached类似,但是解决 ...

  10. mybatis结合generator进行分页插件PluginAdapter开发

    使用org.mybatis.generator生成UserExample时,无法进行分页,使用下面这个类运行generator便可以生成分页相关的属性了 package org.mybatis.gen ...