hdu2768Cat vs. Dog (反建法,最大独立集)

Posted mfmdaoyou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu2768Cat vs. Dog (反建法,最大独立集)相关的知识,希望对你有一定的参考价值。

Cat vs. Dog

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


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
解题:如今是要求满足最多人数的要求,从正面建图有困难。那么反建法,图的每一条边都是不可取的情况。而我们要求的是可取的情况,所以就变成了求最大独立集问题。
首先把v个人分成两个集合。一个集合是爱猫,还有一个是爱狗。这样每一个集合内部没有冲突。然后用反建法建图,cat[i].love==dog[j].hate 或 cat[i].heat==dog[j].love.建边。建完后就按常规求出了。
#include<stdio.h>
#include<string.h>
struct nn
{
    char love[5];
    char hate[5];
}cat[505],dog[505];
int map[505][505],vist[505],match[505],dog_n;
int find(int i)
{
    for(int j=1;j<=dog_n;j++)
    if(!vist[j]&&map[i][j])
    {
        vist[j]=1;
        if(match[j]==0||find(match[j]))
        {
            match[j]=i; return 1;
        }
    }
    return 0;
}
int main()
{
    int t,n,m,v,cat_n;
    char ch1[5],ch2[5];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&v);
        cat_n=0; dog_n=0;
        for(int i=1;i<=v;i++)
        {
            scanf("%s%s",ch1,ch2);
            if(ch1[0]=='C')
            {
                strcpy(cat[++cat_n].love,ch1);
                strcpy(cat[cat_n].hate,ch2);
            }
            else
            {
                strcpy(dog[++dog_n].love,ch1);
                strcpy(dog[dog_n].hate,ch2);
            }
        }
        memset(map,0,sizeof(map));
        for(int i=1;i<=cat_n;i++)
        for(int j=1;j<=dog_n;j++)
        if(strcmp(cat[i].love,dog[j].hate)==0||strcmp(cat[i].hate,dog[j].love)==0)
                    map[i][j]=1;
        memset(match,0,sizeof(match));
        int ans=0;
        for(int i=1;i<=cat_n;i++)
        {
            memset(vist,0,sizeof(vist));
            ans+=find(i);
        }
        printf("%d\n",v-ans);
    }
}


以上是关于hdu2768Cat vs. Dog (反建法,最大独立集)的主要内容,如果未能解决你的问题,请参考以下文章

hdu2768 Cat vs. Dog最大独立集

HDU3829 Cat VS Dog

HDU--3829--Cat VS Dog最大点独立集

HDU3829 Cat VS Dog —— 最大独立集

Cat VS Dog HDU - 3829 (最大独立集 )

HDU3829 Cat VS Dog(最大独立集)