hdu 2768 Cat vs. Dog【最大独立集】

来源:互联网 发布:如何建立大数据平台 编辑:程序博客网 时间:2024/06/06 03:48

Cat vs. Dog

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2008    Accepted Submission(s): 762

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

NWERC 2008

 

 题目大意:有t组数据,有c只猫,d只狗,v个观众。这些观众中,喜欢第一种动物,讨厌第二种动物,如果其喜欢的动物不在了,或者是讨厌的动物在场上,他就会走,问最多能够留下多少人。


思路:

首先庆祝又一道经典好题毁在我的贱手之下。


1、我们将观众之间想办法建立关系,如果观众A喜欢猫xx,不喜欢狗dd,并且右关总B喜欢狗dd,讨厌猫yy,那么这两个人,只能留下一个人,因为如果A喜欢的猫xx不在了,那么A就会走,如果A想留下,那么狗dd就不能在场上,那么B就会走,所以他俩之间就可以建立一个关系,如果A留下了,B就不能留下。


2、那么我们就可以建立观众之间的联系 ,接下来再求一个最大独立集即可。


AC代码:

#include<stdio.h>#include<string.h>using namespace std;int map[550][550];int match[550];int people[550][2];int vis[550];int n,m,k;int find(int x){    for(int i=0;i<k;i++)    {        if(map[x][i]==1&&vis[i]==0)        {            vis[i]=1;            if(match[i]==-1||find(match[i]))            {                match[i]=x;                return 1;            }        }    }    return 0;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        memset(match,-1,sizeof(match));        memset(map,0,sizeof(map));        scanf("%d%d%d",&n,&m,&k);        for(int i=0;i<k;i++)        {            char a[50];            char b[50];            scanf("%s%s",a,b);            int x=0,y=0;            for(int j=1;j<strlen(a);j++)            {                x=x*10+a[j]-'0';            }            for(int j=1;j<strlen(b);j++)            {                y=y*10+b[j]-'0';            }            if(a[0]=='D')x=-x;            if(b[0]=='D')y=-y;            people[i][0]=x;            people[i][1]=y;        }        for(int i=0;i<k;i++)        {            for(int j=i+1;j<k;j++)            {                if(people[i][0]==people[j][1]||people[i][1]==people[j][0])                {                    map[i][j]=1;                    map[j][i]=1;                }            }        }        int output=0;        for(int i=0;i<k;i++)        {            memset(vis,0,sizeof(vis));            if(find(i))output++;        }        printf("%d\n",k-output/2);    }}






0 0
原创粉丝点击