Cat vs. Dog

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

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
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  2767 2769 2772 2770 2773 
 

好好地一题最大独立集被我毁了...

开始思路匹配错了.后来才想清楚,人应该和人匹配,构图原则是一个人喜欢的和另一个人不喜欢的一样则两者匹配,最后求最大独立集。

最大独立集=原点数-最大匹配/2(构图时构双向图);

 //93MS    1780K    1160 B    C++
#include<iostream>
#include<vector>
#include<string.h>
#define N 505
using namespace std;
int match[N];
int vis[N];
int n,m,k;
vector<int>V[N];
int dfs(int u)
{
for(int i=;i<V[u].size();i++){
int v=V[u][i];
if(!vis[v]){
vis[v]=;
if(match[v]==- || dfs(match[v])){
match[v]=u;
return ;
}
}
}
return ;
}
int hungary()
{
int ret=;
memset(match,-,sizeof(match));
for(int i=;i<k;i++){
memset(vis,,sizeof(vis));
ret+=dfs(i);
}
return ret;
}
int main(void)
{
char a[N][],b[N][];
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=k;i++) V[i].clear();
for(int i=;i<k;i++)
scanf("%s%s",&a[i],&b[i]);
for(int i=;i<k;i++)
for(int j=;j<k;j++){
if(strcmp(a[i],b[j])== || strcmp(a[j],b[i])==){
V[i].push_back(j);
V[j].push_back(i);
}
}
printf("%d\n",k-hungary()/);
}
return ;
}

最新文章

  1. 发布有礼!2015 Autodesk程序商店有奖发布活动拉开序幕
  2. MYSQL的常用命令和增删改查语句和数据类型【转】
  3. miniprofiler的对数据库的监测使用。以nancy,petapoco为例
  4. poj 2031Building a Space Station(几何判断+Kruskal最小生成树)
  5. hdu1018
  6. 自定义JSTL标签和函数库
  7. 【Avalon】escape
  8. PHP包名解释
  9. sql连接字符串的方法
  10. HDU 1677
  11. 面试题总结之JAVA
  12. C#依据word模版动态生成文档
  13. Servlet中文乱码问题解决办法
  14. Windows Server 2016-Powershell加域并指定OU (二)
  15. freekan5.9电影网站安装及源码分享
  16. 正则re模块
  17. javascript 小方块平移
  18. 本地搭建ELK(elasticsearch, logstash, kibana)日志收集系统
  19. webpack 打包调试
  20. 摘记—Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Cha

热门文章

  1. 从PRISM开始学WPF(七)MVVM(三)事件聚合器EventAggregator-更新至Prism7.1
  2. 6、Java并发编程:volatile关键字解析
  3. HTML5心得
  4. react组件性能
  5. RSA加密通信小结(二)-新版本APP与后台通信交互内容修改方案
  6. 用libevent实现的echo服务器及telnet客户端
  7. Python字符串操作大全(非常全!!!)
  8. unity实现框选效果
  9. JAVA基础学习之路(二)方法定义,重载,递归
  10. ionic typescript--验证码发送倒计时功能