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. LibreOJ #6221. 幂数 !(数论+dfs+剪枝)

    写新题然后艹翻标程的感觉真是舒爽啊... 这题就是个dfs...先预处理出sqrt(n)范围内的素数,然后dfs构造合法的数就行了. 直接暴搜会TLE,需要剪一剪枝,不需要跑到最后一层再计算答案,边构 ...

  2. finetune on caffe

    官方例程:http://caffe.berkeleyvision.org/gathered/examples/finetune_flickr_style.html 相应的中文说明:http://blo ...

  3. HDU 4417 离线+树状数组

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. [nginx]nginx rewrite规则之last和break

    c俺靠这篇博文 http://eyesmore.iteye.com/blog/1142162 有用的配置: 1.开启rewrite_log,这样在/var/log/nginx/error.log中显示 ...

  5. main函数的传参与返回

    1.谁给main函数传参(1)调用main函数所在的程序的它的父进程给main函数传参,并且接收main的返回值.2.为什么需要给main函数传参(1)首先,main函数不传参是可以的,也就是说父进程 ...

  6. Jade模板引擎学习(一)安装及基本语法

    Jade是一款高性能简洁易懂的模板引擎,Jade是Html的Javascript实现,在服务端(NodeJS)及客户端均有支持. 一.功能 客户端支持  超强的可读性 灵活易用的缩进 块扩展 代码默认 ...

  7. jquery多组图片层次切换的焦点图

    效果:

  8. C11性能之道:转移和转发

    1.move C++11中可以将左值强制转换为右值,从而避免对象的拷贝来提升性能.move将对象的状态或者所有权从一个对象转移到另一个对象,没有内存拷贝.深拷贝和move的区别如图: 从图可以看出,深 ...

  9. 【LibreOJ】#541. 「LibreOJ NOIP Round #1」七曜圣贤

    [题意]一开始车上有编号为0~a的红茶,过程中出现的红茶编号仅有[0,b),有三种操作: 1.买进编号未在车上出现过的红茶. 2.丢掉车上指定编号的红茶. 3.将最早丢出去的红茶捡回来. 每次操作后求 ...

  10. Html5学习3(拖放、Video(视频)、Input类型(color、datetime、email、month 、number 、range 、search、Tel、time、url、week ))

    1.Html拖放 <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> < ...