题目链接: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. Python 单向队列Queue模块详解

    Python 单向队列Queue模块详解 单向队列Queue,先进先出 '''A multi-producer, multi-consumer queue.''' try: import thread ...

  2. python学习笔记--python数据类型

    一.整形和浮点型 整形也就是整数类型(int)的,在python3中都是int类型,没有什么long类型的,比如说存年龄.工资.成绩等等这样的数据就可以用int类型,有正整数.负整数和0,浮点型的也就 ...

  3. [luoguP2015] 二叉苹果树(DP)

    传送门 貌似是个树形背包... 好像吧.. f[i][j]表示节点i选条边的最优解 #include <cstdio> #include <cstring> #include ...

  4. TYVJ P 1214 硬币问题

    TYVJ  P 1214 硬币问题 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述   有n种硬币,面值为别为a[1],a[2],a[3]……a[n],每种都 ...

  5. 【2018 Multi-University Training Contest 3】

    01:https://www.cnblogs.com/myx12345/p/9420198.html 02: 03: 04:https://www.cnblogs.com/myx12345/p/940 ...

  6. Bootstrap3 为何无法显示Glyphicons 图标

    Bootstrap3 为何无法显示Glyphicons 图标 在CSS引入字体即可解决 @font-face { font-family: 'Glyphicons Halflings'; src: u ...

  7. [Bzoj2733][Hnoi2012] 永无乡(BST)(Pb_ds tree)

    2733: [HNOI2012]永无乡 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 4108  Solved: 2195[Submit][Statu ...

  8. (转)Redis

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

  9. Java中文件和I/O

    以下内容引用自http://wiki.jikexueyuan.com/project/java/files-and-io.html: 在Java中java.io包含的每一个类几乎都要进行输入和输出操作 ...

  10. jsoup 提取 html 中的所有链接、图片和媒体

    原文:http://www.open-open.com/code/view/1420729333515 package org.jsoup.examples; import org.jsoup.Jso ...