hdu 2768 Cat vs. Dog (二分匹配)

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

NWERC
2008

Recommend

lcy   |   We have
carefully selected several similar problems for you:  2767 2769 2772 2770 2773

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

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

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


 1 //93MS    1780K    1160 B    C++
2 #include<iostream>
3 #include<vector>
4 #include<string.h>
5 #define N 505
6 using namespace std;
7 int match[N];
8 int vis[N];
9 int n,m,k;
10 vector<int>V[N];
11 int dfs(int u)
12 {
13 for(int i=0;i<V[u].size();i++){
14 int v=V[u][i];
15 if(!vis[v]){
16 vis[v]=1;
17 if(match[v]==-1 || dfs(match[v])){
18 match[v]=u;
19 return 1;
20 }
21 }
22 }
23 return 0;
24 }
25 int hungary()
26 {
27 int ret=0;
28 memset(match,-1,sizeof(match));
29 for(int i=0;i<k;i++){
30 memset(vis,0,sizeof(vis));
31 ret+=dfs(i);
32 }
33 return ret;
34 }
35 int main(void)
36 {
37 char a[N][5],b[N][5];
38 int t;
39 scanf("%d",&t);
40 while(t--)
41 {
42 scanf("%d%d%d",&n,&m,&k);
43 for(int i=0;i<=k;i++) V[i].clear();
44 for(int i=0;i<k;i++)
45 scanf("%s%s",&a[i],&b[i]);
46 for(int i=0;i<k;i++)
47 for(int j=0;j<k;j++){
48 if(strcmp(a[i],b[j])==0 || strcmp(a[j],b[i])==0){
49 V[i].push_back(j);
50 V[j].push_back(i);
51 }
52 }
53 printf("%d\n",k-hungary()/2);
54 }
55 return 0;
56 }

hdu 2768 Cat vs. Dog (二分匹配)

时间: 2024-10-13 12:25:42

hdu 2768 Cat vs. Dog (二分匹配)的相关文章

hdu 3829 Cat VS Dog 二分匹配 最大独立点集

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3829 题目大意: 给定N个猫,M个狗,P个小朋友,每个小朋友都有喜欢或者不喜欢的某猫或者某狗 管理员从中删除一些猫狗,使得尽可能多的小朋友开心 思路: 假设A小朋友喜欢的猫是B小朋友所不喜欢的,或者说A不喜欢的狗是B喜欢的,那么说明两者之间存在矛盾关系 问题就是求出互相之间没有矛盾的小朋友的集合 那么就是点数-最大匹配数的问题了,就是读入数据有点麻烦 代码: 1 #include <iostream

hdu 3829 Cat VS Dog 二分图匹配 最大点独立集

Cat VS Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Problem Description The zoo have N cats and M dogs, today there are P children visiting the zoo, each child has a like-animal and a dislike-animal, if the

HDU——2768 Cat vs. Dog

Cat vs. Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2279    Accepted Submission(s): 886 Problem Description The latest reality show has hit the TV: ``Cat vs. Dog''. In this show, a bunc

hdu 2768 Cat vs. Dog 最大独立集 巧妙的建图

题目分析: 一个人要不是爱狗讨厌猫的人,要不就是爱猫讨厌狗的人.一个人喜欢的动物如果离开,那么他也将离开.问最多留下多少人. 思路: 爱猫和爱狗的人是两个独立的集合.若两个人喜欢和讨厌的动物是一样的,那么就建一条边.留下多少人,就是求最大独立集. 最大独立集= 顶点数 - 最大匹配数 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #inc

HDU 3829 - Cat VS Dog (二分图最大独立集)

题意:动物园有n只猫和m条狗,现在有p个小孩,他们有的喜欢猫,有的喜欢狗,其中喜欢猫的一定不喜欢狗,喜欢狗的一定不喜欢猫.现在管理员要从动物园中移除一些动物,如果一个小孩喜欢的动物留了下来而不喜欢的动物被移走,这个小孩会很高兴.现在问最多可以让多少个小孩高兴. 此题是求二分图最大独立集. 二分图比较明显,但是难在建图.这个题是找到最多的喜欢猫和喜欢狗而不互相冲突的小孩,这样我们将喜欢动物相互冲突的小孩之间连边,问题就变成了求二分图的最大独立集. 在二分图中,最大独立集=顶点数-最大匹配数. 求解

HDU 3289 Cat VS Dog (二分匹配 求 最大独立集)

题意:每个人有喜欢的猫和不喜欢的狗.留下他喜欢的猫他就高心,否则不高心.问最后最多有几个人高心. 思路:二分图求最大匹配 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 #include<cstdlib> 6 #include<string> 7 #include<cmath> 8 #include<

hdu 5093 Battle ships 最大二分匹配

Battle ships Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 589    Accepted Submission(s): 233 Problem Description Dear contestant, now you are an excellent navy commander, who is responsible

HDU - 1045 Fire Net(二分匹配)

Description Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a small castle that has four openings through which to s

hdu 4619 Warm up 2 (二分匹配)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4619 题意: 平面上有一些1×2的骨牌,每张骨牌要么水平放置,要么竖直放置,并且保证同方向放置的骨牌不会相互覆盖.水平放置的牌和竖直放置的牌可能相互覆盖,现在要移去一些牌,使得剩下的牌任何两张都不会相互覆盖,问桌面上最多能剩多少张牌. 分析: 如果把每张牌看作一个结点,则共有两类结点,容易联想到二分图.另外,同方向的牌不会相互覆盖,不同方向的可能相互覆盖,易想到二分图的一个重要性质:同类结点间不会连