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 ;
}
 
 

最新文章

  1. Spring中配置数据源的4种形式
  2. Delphi 获取时间的年月日
  3. Android 快速开发框架:推荐10个框架:afinal、ThinkAndroid、andBase、KJFrameForAndroid、SmartAndroid、dhroid..
  4. 小白日记43:kali渗透测试之Web渗透-SqlMap自动注入(一)-sqlmap参数详解TARGET
  5. Centos7最小化安装后(minimal)安装图形界面
  6. SqlServer 开启或关闭数据库主键自增
  7. poj1552
  8. iOS加密个人见解
  9. javacript参数传递表单验证
  10. 如何延长zencart1.5后台的登录时间而不退出
  11. HTML笔记&lt;note2&gt;
  12. [SCOI2007]最大土地面积
  13. Mac安装软件包管理工具Homebrew
  14. python数据结构(二)------列表
  15. java的String的乱码浅析
  16. Iptables防火墙规则使用
  17. 给定两个数组,这两个数组是排序好的,让你求这两个数组合到一起之后第K大的数。
  18. PAT甲级 1126. Eulerian Path (25)
  19. Populating Next Right Pointers in Each Node leetcode java
  20. VBA 打开带密码的文件

热门文章

  1. 在Spring中使用异步事件实现同步事务
  2. 在Build时使用NuGet自动下载缺失的包
  3. linux串口驱动分析
  4. resin config 中文(resin.xml)
  5. 自定义ImageView实现图片手势滑动,多点触摸放大缩小效果
  6. Android 推断当前Activity是不是最后一个Activity 以及 应用或Activity是否存在
  7. Python-elementTree方法解析xml文件-01
  8. DataGrid( 数据表格) 组件[7]
  9. webview笔记
  10. 《Linux多线程服务器端编程》读书笔记第3章