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.
* 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 the maximum possible number of satisfied voters for the show.
2 1 1 2 C1 D1 D1 C1 1 2 4 C1 D1 C1 D1 C1 D2 D2 C1
1 3
题解:
题意:
猫狗选美,给n条狗,m只猫和v个人,如何是v行,每行有一只他讨厌的猫和喜欢的狗或者是喜欢的猫和讨厌的狗,最终让你求最多能够满足多少人的需求
思路:
之前没有做过这种题。。看到这题手足无措,知道用二分匹配,可是不知道如何建边,后来看题解才知道直接根据人的喜好,当一个人喜欢的东西和另一个人不喜欢的东西出现时其中的一个就会不满,而一个人的满足就是他喜欢的上场了,根据这一点我们可以进行最大匹配,我们把投票人分成喜欢猫和喜欢狗的两种,题目并没有说要让多少猫和狗上场,我们将可以产生不满的人匹配到一起,那么只要把所有满意的情况减去这些不满的情况就可以达到满足最大化,也就是最大独立集=顶点数-最大匹配数
代码:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<deque>
#define M (t[k].l+t[k].r)/2
#define lson k*2
#define rson k*2+1
#define ll long long
using namespace std;
char like[505][10];
char dislike[505][10];
int p[505][505];
int used[505];
int vis[505];
int n,m,v;
int find(int u)//匈牙利算法
{
int i;
for(i=1;i<=v;i++)
{
if(!vis[i]&&p[u][i])
{
vis[i]=1;
if(!used[i]||find(used[i]))
{
used[i]=u;
return 1;
}
}
}
return 0;
}
int main()
{
int i,j,k,test;
scanf("%d",&test);
while(test--)
{
scanf("%d%d%d",&n,&m,&v);
for(i=1;i<=v;i++)
{
scanf("%s%s",like[i],dislike[i]);
}
memset(p,0,sizeof(p));
for(i=1;i<=v;i++)
{
for(j=1;j<=v;j++)
{
if(strcmp(like[i],dislike[j])==0||strcmp(like[j],dislike[i])==0)//两人的喜欢有矛盾
p[i][j]=1;//这两人建边
}
}
int ans=0;
memset(used,0,sizeof(used));
for(i=1;i<=v;i++)//求最大匹配数,由于我们是建了双向的边,所以匹配数目也是双倍的,最后答案要除2就是匹配出来矛盾的人
{
memset(vis,0,sizeof(vis));
if(find(i))
ans++;
}
ans/=2;
printf("%d\n",v-ans);//所有满足的情况-矛盾的情况就是满足的情况
}
return 0;
}