Cat vs. Dog

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1602    Accepted Submission(s): 606

Problem Description
The
latest reality show has hit the TV: ``Cat vs. Dog''. In this show, a
bunch of cats and dogs compete for the very prestigious Best Pet Ever
title. In each episode, the cats and dogs get to show themselves off,
after which the viewers vote on which pets should stay and which should
be forced to leave the show.

Each viewer gets to cast a vote on
two things: one pet which should be kept on the show, and one pet which
should be thrown out. Also, based on the universal fact that everyone is
either a cat lover (i.e. a dog hater) or a dog lover (i.e. a cat
hater), it has been decided that each vote must name exactly one cat and
exactly one dog.

Ingenious as they are, the producers have
decided to use an advancement procedure which guarantees that as many
viewers as possible will continue watching the show: the pets that get
to stay will be chosen so as to maximize the number of viewers who get
both their opinions satisfied. Write a program to calculate this maximum
number of viewers.

 
Input
On the first line one positive number: the number of testcases, at most 100. After that per testcase:

* One line with three integers c, d, v (1 ≤ c, d ≤ 100 and 0 ≤ v ≤ 500): the number of cats, dogs, and voters.
* v lines with two pet identifiers each. The first is the pet that
this voter wants to keep, the second is the pet that this voter wants to
throw out. A pet identifier starts with one of the characters `C' or
`D', indicating whether the pet is a cat or dog, respectively. The
remaining part of the identifier is an integer giving the number of the
pet (between 1 and c for cats, and between 1 and d for dogs). So for
instance, ``D42'' indicates dog number 42.

 
Output
Per testcase:

* One line with the maximum possible number of satisfied voters for the show.

 
Sample Input
2
1 1 2
C1 D1
D1 C1
1 2 4
C1 D1
C1 D1
C1 D2
D2 C1
 
Sample Output
1
3
 
Source

算法:将喜欢猫和喜欢狗的人分开,就形成了一个二分图,这样喜欢猫(狗)的人一定不会冲突的,然后扫描一次,将互相矛盾的人连一条边。
二分图的最大独立集就是答案。由已知定理有:二分图最大独立集=顶点数-二分图最大匹配。求二分图最大匹配即可。
 
 
 const int MAXN = ;
int uN,vN;//u,v的数目,使用前面必须赋值
int g[MAXN][MAXN];//邻接矩阵
int linker[MAXN];
bool used[MAXN];
#include <memory.h>
bool dfs(int u)
{
for(int v = ; v < vN; v++)
if(g[u][v] && !used[v])
{
used[v] = true;
if(linker[v] == - || dfs(linker[v]))
{
linker[v] = u;
return true;
}
}
return false;
}
int hungary()
{
int res = ;
memset(linker,-,sizeof(linker));
for(int u = ; u < uN; u++)
{
memset(used,false,sizeof(used));
if(dfs(u))res++;
}
return res;
} #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std; int getint(char s[])
{
int ans=;
for(int i=; i<strlen(s); i++)
{
ans=ans*+s[i]-'';
}
return ans;
} typedef pair<int,int> CPair; CPair upair[],vpair[];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
int T;
cin>>T;
while(T--)
{
memset(g,,sizeof g);
int nc,nd,n;
uN=vN=;
scanf("%d%d%d",&nc,&nd,&n);
for(int i=; i<n; i++) // 人的数量
{
char s1[],s2[];
scanf("%s%s",s1,s2); // C1 D1
int ci=getint(s1+),di=getint(s2+);
if(s1[]=='C')
{
// 放到第一个集合
upair[uN++]=CPair(ci,di);
}
else
{
vpair[vN++]=CPair(ci,di);
}
}
// 构图
for(int i=;i<uN;i++)
for(int j=;j<vN;j++)
{
// 矛盾的话就连一条边
if(upair[i].first==vpair[j].second || upair[i].second==vpair[j].first)
g[i][j]=;
} int ans=uN+vN-hungary();
printf("%d\n",ans);
}
return ;
}
 
 

hdu2768-Cat vs. Dog:图论:二分匹配的更多相关文章

  1. HDU 3289 Cat VS Dog (二分匹配 求 最大独立集)

    题意:每个人有喜欢的猫和不喜欢的狗.留下他喜欢的猫他就高心,否则不高心.问最后最多有几个人高心. 思路:二分图求最大匹配 #include<cstdio> #include<cstr ...

  2. poj 1034 The dog task (二分匹配)

    The dog task Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2559   Accepted: 1038   Sp ...

  3. HDU-2768 Cat vs. Dog

    题意一开始是理解错的...结果就各种WA啦~ 对于两个观众,假如有某只宠物,一个人讨厌另一个人却喜欢,这两个人就是有矛盾的,连边. 最后求最小顶点覆盖.因为把这个覆盖点集去掉的话剩下的图中没有两个点是 ...

  4. hdu 2768 Cat vs. Dog (二分匹配)

    Cat vs. Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. hdu 3829 Cat VS Dog 二分图匹配 最大点独立集

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

  6. [ACM_图论] Sorting Slides(挑选幻灯片,二分匹配,中等)

    Description Professor Clumsey is going to give an important talk this afternoon. Unfortunately, he i ...

  7. [ACM_图论] The Perfect Stall 完美的牛栏(匈牙利算法、最大二分匹配)

    描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们 ...

  8. kuangbin带你飞 匹配问题 二分匹配 + 二分图多重匹配 + 二分图最大权匹配 + 一般图匹配带花树

    二分匹配:二分图的一些性质 二分图又称作二部图,是图论中的一种特殊模型. 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j ...

  9. 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

随机推荐

  1. EXP/IMP的三种模式

    基本语法和实例:     1.EXP:       有三种主要的方式(完全.用户.表)       1.完全:           EXP SYSTEM/MANAGER BUFFER=64000 FI ...

  2. UIWebView加载html 图片大小自适应的方法汇总

    方法一 处理HTMLString的方法: NSString *htmls = [NSString stringWithFormat:@"<html> \n" " ...

  3. Foreach语法

    先看例子: Random rand = new Random(47); float f[] = new float[10]; for(int i = 0; i < 10; i++){ f[i] ...

  4. Mac打造python2 python3开发环境

    最新版的MacOS Sierra默认带的python环境仍然为2.7.10,本文将介绍使用Brew安装python3.5并做简单的配置,打造python2,python3共存的开发环境 直接尝试bre ...

  5. input事件以及中文输入法的处理

    在项目的开发过程中,相信大家都处理过监听用户输入的事情,一般我们会用到onkeyup.onkeydown.onkeypress.onchange.oninput事件,虽然都很熟悉了,但是还是有必要巩固 ...

  6. 国外十个出名的 upload 上传组件

    在日常开发中,我们常会用到很多的组件及共用代码提高我们的开发效率.   King MEDIA - $ 17.00 / 11 Sales DNNStore | 6/5/2014 6:06:42 PM|  ...

  7. [每日一题] OCP1z0-047 :2013-07-25 权限――角色与对象权限

    有疑问可以去itpub讨论:http://www.itpub.net/thread-1804842-1-1.html 按题意,操作如下: 1.创建一个角色r1 sys@OCM> create r ...

  8. C# HashSet类(复杂)对象的去重

    public class Student { public string Id { get; set; } public string Name { get; set; } public overri ...

  9. web标准(复习)--2 列布局

    今天我们开始学习一列布局,包含以下几种形式: 1.一列固定宽度 2.一列固定宽度居中 3.一列自适应宽度 4.一列自适应宽度居中 5.一列二至多块布局 前一节我们回顾了xhtml基础和css基础部分, ...

  10. [Mugeda HTML5技术教程之9]使用元件

    元件是一个可以在舞台上实例化和再利用的预先生成的独立动画.一个元件有它自己的时间轴(层,单位等),可以独立显示的动画.元件提高了动画的重用性和灵活性,是个强大的存在.元件可用于创建复杂的动画效果. 所 ...