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

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



题意:

找一群猫和狗来表演,然后由观众来投票选择其中一只留下、一只淘汰,且每一个观众的两个选择必然是不同种类的动物(一猫一狗)。如果一个观众的两项选择没有被全部满足的话,那么他就不会再继续收看这个节目了。现在要在已有的动物中选择一部分留下来、一部分淘汰(也有可能没有被淘汰的),要使得留下的观众数量最多。


思路:把喜欢猫和喜欢狗的把观众分为两个集合。只要两个集合内的人的选择有冲突,这两个顶点连接,边代表矛盾,然后求最大独立集。

  最大独立集 = 顶点数 - 最小顶点覆盖数(最大匹配数)

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node{
    char c1[5],c2[5];
}str1[505],str2[505];
int n,m,k;
int dog,cat;
int link[505],vis[505];
int map[505][505];
int find(int u)
{
    for(int v=1;v<dog;v++)
    {
        if(map[u][v]&&!vis[v])
        {
            vis[v]=1;
            if(!link[v]||find(link[v]))
            {
                link[v]=u;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int t;
    scanf("%d",&t);
    char c1[5],c2[5];
    while(t--)
    {
        cat=1;dog=1;
        scanf("%d%d%d",&n,&m,&k);
        for(int i=1;i<=k;i++)
        {
            scanf("%s%s",c1,c2);
            if(c1[0]=='C')
            {
                strcpy(str1[cat].c1,c1);
                strcpy(str1[cat].c2,c2);
                cat++;
            }
            else
            {
                strcpy(str2[dog].c1,c1);
                strcpy(str2[dog].c2,c2);
                dog++;
            }
        }
        memset(map,0,sizeof(map));
        memset(link,0,sizeof(link));
        for(int i=1;i<cat;i++)
            for(int j=1;j<dog;j++)
            {
                if(!strcmp(str1[i].c1,str2[j].c2)||!strcmp(str1[i].c2,str2[j].c1))
                        map[i][j]=1;
            }
        int s=0;
        for(int i=1;i<cat;i++)
        {
            memset(vis,0,sizeof(vis));
            s+=find(i);
        }
        printf("%d\n",k-s);
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值