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): 2026    Accepted Submission(s): 771


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
题意:有n个人,有自己喜欢的动物和讨厌的动物,总共有两种动物猫和狗,喜欢的和讨厌的一定是两种不同的动物

现在要让尽可能多的人满意(喜欢的在场上,讨厌的不在场上),问最多可以满足多少个人?

思路:要求有多少个人满足,这道题就不能单单从动物入手。

我们将x集合定为人,y集合也定为人,然后把不能共存的人连一条边,最后求一次最大独立集即可

独立集里面的人表示任意两个人之间都可以共存

因为x集合和y集合表示的意义相同,故要连双向边, 最后结果等于n-ans/2

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 510
#define INF 999999999
int ma[N][N];
int n,m,k,line[N],vis[N];
struct Node
{
    char c,d;
    int x,y;
} p[N];
int can(int t)
{
    for(int i=1; i<=k; i++)
    {
        if(i==t)continue;
        if(!vis[i]&&ma[t][i])
        {
            vis[i]=1;
            if(line[i]==-1||can(line[i]))
            {
                line[i]=t;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d %d",&n,&m,&k);
        memset(line,-1,sizeof(line));
        memset(ma,0,sizeof(ma));
        getchar();
        for(int i=1; i<=k; i++)
        {
            scanf("%c%d %c%d",&p[i].c,&p[i].x,&p[i].d,&p[i].y);
            getchar();
        }
        for(int i=1; i<=k; i++)
            for(int j=1; j<=k; j++)
                if((p[i].c==p[j].d&&p[i].x==p[j].y)||(p[i].d==p[j].c&&p[i].y==p[j].x))
                    ma[i][j]=1;
        int ans=0;
        for(int i=1; i<=k; i++)
        {
            memset(vis,0,sizeof(vis));
            if(can(i)) ans++;
        }
        printf("%d\n",k-ans/2);
    }
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值