HDU2768Cat vs. Dog二分图 最大独立集(最大匹配)
来源:互联网 发布:压力校验台淘宝 编辑:程序博客网 时间:2024/06/11 04:47
今天做的二分图唯一一个比较难的 而且看了题解才发现,又理解错题意了。是说 如果出现喜欢的和别人讨厌的相同,则其中一人会不满意。问最多满意多少人
即把喜欢喝讨厌彼此矛盾的建边,个数-最大匹配即为所求
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.
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.
* 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.
* One line with the maximum possible number of satisfied voters for the show.
Sample Input
21 1 2C1 D1D1 C11 2 4C1 D1C1 D1C1 D2D2 C1
Sample Output
13
Source
#include <iostream>#include <stdio.h>#include <string.h>#define M 550#define inf 0x3f3f3f3fusing namespace std;const int MAXN=550;int uN,vN;//u,v数目int g[MAXN][MAXN];int linker[MAXN];bool used[MAXN];bool dfs(int u)//从左边开始找增广路径{ int v; for(v=0;v<vN;v++)//这个顶点编号从0开始,若要从1开始需要修改 if(g[u][v]&&!used[v]) { used[v]=true; if(linker[v]==-1||dfs(linker[v])) {//找增广路,反向 linker[v]=u; return true; } } return false;//这个不要忘了,经常忘记这句}int hungary(){ int res=0; int u; memset(linker,-1,sizeof(linker)); for(u=0;u<uN;u++) { memset(used,0,sizeof(used)); if(dfs(u)) res++; } return res;}char like[550][10],dislike[550][10];int main(){ // freopen("cin.txt","r",stdin); int T,c,d,v; while(~scanf("%d",&T)) { while(T--) { scanf("%d%d%d",&c,&d,&v); vN=uN=v; for(int i=0;i<v;i++) scanf("%s%s",like[i],dislike[i]); memset(g,0,sizeof(g)); for(int i=0;i<v;i++) for(int j=0;j<v;j++) { if(strcmp(like[i],dislike[j])==0||strcmp(like[j],dislike[i])==0) g[i][j]=1; } printf("%d\n",v-hungary()/2); } } return 0;}
0 0
- HDU2768Cat vs. Dog二分图 最大独立集(最大匹配)
- hdu2768Cat vs. Dog (反建法,最大独立集)
- HDU2768Cat vs. Dog(最大独立集)
- hdu2768Cat VS Dog(最大独立集)
- hdu3829Cat VS Dog 二分匹配求最大独立集
- hdu 2768 Cat vs. Dog(二分图最大匹配--最大独立点集)
- HDU 3829 Cat VS Dog(最大独立集|二分图最大匹配)
- HDU 3829 - Cat VS Dog【二分图最大匹配最大独立集】
- UVA 12168 - Cat vs. Dog(二分图匹配+最大独立集)
- UVA 12168 Cat vs. Dog(二分图匹配+匈牙利算法+最大独立集+数据转化)
- Cat VS Dog 求二分图最大独立集
- HDU 3829 Cat VS Dog(二分图最大独立集)
- HDU 3829 Cat VS Dog(二分图最大独立集)
- HDU3829Cat VS Dog(二分图最大独立集)
- HDU 3829 Cat VS Dog(二分图最大独立集)
- HDU3829 Cat VS Dog(二分图最大独立集)
- hdu 2768 Cat vs. Dog 最大独立集(最大匹配)
- HDU 3829 Cat VS Dog 最大独立集(最大匹配)
- 黑马程序员--正则表达式
- NMAKE : fatal error U1045: spawn failed : Permission denied
- SDM对分频器输出信号相位噪声的影响
- SQL中的case when then else end用法
- HDOJ 3665 Seaside(最短路--floyd)
- HDU2768Cat vs. Dog二分图 最大独立集(最大匹配)
- [不断更新]前端工程师面试必备知识清单
- python发送Email 带附件
- UVa 1613:K-Graph Oddity(BFS)
- 字典数据类型-字典常见操作
- RPC和NFS还有如此的渊源TODO
- 学习Spring必学的Java基础知识(7)----事务基础知识
- Android数据库ORMlite框架翻译系列(第一章)
- HDOJ 5137 How Many Maos Does the Guanxi Worth(最短路)