Cat vs. Dog

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

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
 
题意:有n个人,这些人有人喜欢猫a并且讨厌狗b,有的人喜欢狗c并且讨厌猫d,将这些猫和狗换掉或者留下,问最优情况下满意的人最多有多少?
题解:建图,将人的喜欢与不喜欢建成一个二分图,对于某一只动物,如果甲喜欢乙不喜欢,那么就连一条边,反之也是,由于人是同一个集合,所以连双向边,最终求出最大匹配数,然后求出最大独立集即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include <algorithm>
#include <math.h>
#include <queue>
using namespace std;
const int N = ;
int c,d,v;
char a[N][],b[N][];
int graph[N][N];
int linker[N];
bool vis[N];
bool dfs(int u){
for(int i=;i<=v;i++){
if(graph[u][i]&&!vis[i]){
vis[i] = true;
if(linker[i]==-||dfs(linker[i])){
linker[i] = u;
return true;
}
}
}
return false;
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--){
scanf("%d%d%d",&c,&d,&v);
for(int i=;i<=v;i++){
scanf("%s%s",a[i],b[i]);
}
memset(graph,,sizeof(graph));
for(int i=;i<=v;i++){
for(int j=;j<=v;j++){
if(strcmp(a[i],b[j])==||strcmp(a[j],b[i])==){
graph[i][j] = graph[j][i] = ;
}
}
}
int res = ;
memset(linker,-,sizeof(linker));
for(int i=;i<=v;i++){
memset(vis,false,sizeof(vis));
if(dfs(i)) res++;
}
printf("%d\n",v-res/);
}
return ;
}

hdu 2768(建图,最大点独立集)的更多相关文章

  1. 【POJ】1419:Graph Coloring【普通图最大点独立集】【最大团】

    Graph Coloring Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5775   Accepted: 2678   ...

  2. HDU 4725 建图

    http://acm.hdu.edu.cn/showproblem.php?pid=4725 The Shortest Path in Nya Graph Time Limit: 2000/1000 ...

  3. hdu 4444 Walk (离散化+建图+bfs+三维判重 好题)

    Walk Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submi ...

  4. Food HDU - 4292 网络流 拆点建图

    http://acm.hdu.edu.cn/showproblem.php?pid=4292 给一些人想要的食物和饮料,和你拥有的数量,问最多多少人可以同时获得一份食物和一份饮料 写的时候一共用了2种 ...

  5. hdu 2647 (拓扑排序 邻接表建图的模板) Reward

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2647 老板给员工发工资,每个人的基本工资都是888,然后还有奖金,然后员工之间有矛盾,有的员工希望比某员 ...

  6. HDU 4370 0 or 1(spfa+思维建图+计算最小环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4370 题目大意:有一个n*n的矩阵Cij(1<=i,j<=n),要找到矩阵Xij(i< ...

  7. HDU 4725 The Shortest Path in Nya Graph(spfa+虚拟点建图)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题目大意:有n层,n个点分布在这些层上,相邻层的点是可以联通的且距离为c,还有额外给出了m个条边 ...

  8. HDU 3639 Hawk-and-Chicken(强连通缩点+反向建图)

    http://acm.hdu.edu.cn/showproblem.php?pid=3639 题意: 有一群孩子正在玩老鹰抓小鸡,由于想当老鹰的人不少,孩子们通过投票的方式产生,但是投票有这么一条规则 ...

  9. hdu 5294 Tricks Device 最短路建图+最小割

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5294 Tricks Device Time Limit: 2000/1000 MS (Java/Other ...

随机推荐

  1. CentOS 6.5 下 QT4 连接 mysql 数据库的步骤

    QT4 的安装请参考: CentOS 6.5 下安装 QT 4 mysql 的安装请参考: CentOS 6.5 下安装配置 mysql 1. 预防万一,先安装一下mysql-devel(一定要装!) ...

  2. ACE自适配通信环境简介

    转载于:http://www.cnblogs.com/TianFang/archive/2006/12/03/580795.html ACE自适配通信环境 (Adaptive Communicatio ...

  3. hadoop配置文件详解、安装及相关操作

    一.      Hadoop伪分布配置 1. 在conf/hadoop-env.sh文件中增加:export JAVA_HOME=/home/Java/jdk1.6            2.  在c ...

  4. Rectangle 暴力枚举大法

    frog has a piece of paper divided into n rows and m columns. Today, she would like to draw a rectang ...

  5. 问题03.如果有多个集合的迭代处理情况【使用MAP】

    在SQL开发过程中,动态构建In集合条件查询是比较常见的用法,在Mybatis中提供了foreach功能,该功能比较强大,它允许你指定一个集合,声明集合项和索引变量,它们可以用在元素体内.它也允许你指 ...

  6. SNS应用好友动态Feed模块设计

    转载自:http://libo93122.blog.163.com/blog/static/122189382012112145728902/ 备注:找不到原作者了. 现在大部分SNS网站都有一个功能 ...

  7. ES6中函数的扩展

    一.设置默认参数 ES6之前,给函数设置默认参数是这样做的: function fn(a) { if(typeof y === undefined){ a = a || 'hello'; } cons ...

  8. 2050年这些职业将逐渐被AI(人工智能)取代

    耳熟能详的人工智能   深蓝Deep Blue是美国IBM公司生产的一台超级国际象棋电脑,重1270公斤,有32个大脑(微处理器),每秒钟可以计算2亿步."深蓝”输入了一百多年来优秀棋手的对 ...

  9. LightOJ 1321 - Sending Packets 简单最短路+期望

    http://www.lightoj.com/volume_showproblem.php?problem=1321 题意:每条边都有概率无法经过,但可以重新尝试,现给出成功率,传输次数和传输时间,求 ...

  10. 2015/9/20 Python基础(16):类和实例

    面向对象编程编程的发展已经从简单控制流中按步的指令序列进入到更有组织的方式中,依靠代码块可以形成命名子程序和完成既定的功能.结构化的或过程性编程可以让我们把程序组织成逻辑快,以便重复或重用.创造程序的 ...