UVA 12168 Cat vs. Dog(二分图匹配+匈牙利算法+最大独立集+数据转化)

55 篇文章 0 订阅
16 篇文章 0 订阅

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条狗,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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值