二分(建边))。。

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: 

题意:有v个观众要投票留下一只猫,抛弃一只狗或者留下一只狗,抛弃一只猫,你要尽可能满足他们的要求,问最多能满足多少人的意愿。

思路:是模板,,关键是怎么建边

根据人的喜好,当一个人喜欢的东西和另一个人不喜欢的东西出现时其中的一个就会不满,而一个人的满足就是他喜欢的上场了,根据这一点我们可以进行最大匹配,我们把投票人分成喜欢猫和喜欢狗的两种,题目并没有说要让多少猫和狗上场,我们将可以产生不满的人匹配到一起,那么只要把所有满意的情况减去这些不满的情况就可以达到满足最大化,也就是最大独立集=顶点数-最大匹配数

因为是双向建边,所以结果要除2.。。。。

#include <iostream>
#include <cstring>
#include <map>
using namespace std;
const int maxn=511;
int match[maxn];
int vis[maxn];
int mapp[maxn][maxn];
int c,d,v;
char x[maxn][maxn],y[maxn][maxn];
int find(int x)
{
    for(int i=1;i<=v;i++)
    {
        if(vis[i]==0&&mapp[x][i]==1)
        {
            vis[i]=1;
            if(match[i]==0||find(match[i]))
            {
                match[i]=x;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--)
    {
        cin>>c>>d>>v;
        memset(match,0,sizeof(match));
        memset(mapp,0,sizeof(mapp));
        int sum=0;
        for(int i=1;i<=v;i++)
        {
            cin>>x[i]>>y[i];
        }
        for(int i=1;i<=v;i++)
        {
            for(int j=1;j<=v;j++)
            {
                if(strcmp(x[i],y[j])==0||strcmp(x[j],y[i])==0)
                    mapp[i][j]=1;
            }
        }
        for(int i=1;i<=v;i++)
        {
            memset(vis,0,sizeof(vis));
            if(find(i))
                sum++;
        }
        cout<<v-sum/2<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值