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): 1518    Accepted Submission(s): 568


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

题意:题目主要是求满足尽量多的人要求,也即是有矛盾的人不能共存。

求最大留下的观众,观众之间存在不能同时满足的关系,就是矛盾关系,

矛盾关系建边,建边是双向的所以最大匹配要/2。

主要是建边,刚开始建边弄复杂了,还得继续努力啊!!

#include"stdio.h"
#include"string.h"
#include"queue"
#include"vector"
using namespace std;
#define N 505
#define M 505
int mark[N],link[N],g[N][N],n;
struct node
{
    int like,hate;
}map[N];
int find(int k)
{
    int i;
    for(i=0;i<n;i++)
    {
        if(!mark[i]&&g[k][i])
        {
            mark[i]=1;
            if(link[i]==-1||find(link[i]))
            {
                link[i]=k;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int i,k,c,d,j,a,T;
    char str[10];
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&c,&d,&n);
        for(i=0;i<n;i++)
        {
            for(j=0;j<2;j++)
            {
                scanf("%s",str);
                a=0;
                for(k=1;str[k]!='\0';k++)
                {
                    a*=10;
                    a+=str[k]-'0';
                }
                if(str[0]=='D')
                    a+=100;
                if(j==0)
                    map[i].like=a;
                else
                    map[i].hate=a;
            }
        }
        memset(g,0,sizeof(g));
        for(i=0;i<n;i++)
        {
            for(j=0;j<i;j++)
            {
                if(map[i].like==map[j].hate||map[i].hate==map[j].like)
                    g[i][j]=g[j][i]=1;
            }
        }
        int ans=0;
        memset(link,-1,sizeof(link));
        for(i=0;i<n;i++)
        {
            memset(mark,0,sizeof(mark));
            ans+=find(i);
        }
        printf("%d\n",n-ans/2);
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值