HDU 2768 Cat vs. Dog 最大独立集+匈牙利算法(提高题)

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): 1787 Accepted Submission(s): 676

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' orD’, 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

/*hdu 2768 --------------最大独立集------- 当观众想要留下来的宠物出局时,这个观众才认为会离开 。 以cat_lover和dog_lover把观众分为两个集合。只要两个集合内的人的选择有冲突,这两个顶点连接,边代表矛盾,然后求最大独立集。 最大独立集 = 顶点数 - 最小顶点覆盖数(最大匹配数) */
#include<iostream>
using namespace std;

const int N = 505;

struct option
{
    char a[5], b[5];        //a表示喜欢的动物。b表示不喜欢的动物
};

int maze[N][N];
option cat[N], dog[N];
int isvisit[N];
int match[N];
int c, d, v;
int cnt_cat, cnt_dog;

bool find (int u)                        //匈牙利算法
{
    for (int i = 0; i < cnt_dog; i++)
        if (maze[u][i] && !isvisit[i])
        {
            isvisit[i] = true;
            if (match[i] == -1 || find(match[i]))
            {
                match[i] = u;
                return true;
            }
        }

    return false;
}

int main()
{
    int cases;
    char a[5];
    char b[5];

    scanf("%d", &cases);
    while (cases--)
    {
        scanf("%d%d%d", &c, &d, &v);
        cnt_cat = 0;
        cnt_dog = 0;
        for (int i = 0; i < v; i++)  
        {
            scanf("%s%s", a, b);

            if (a[0] == 'C')
            {
                strcpy(cat[cnt_cat].a, a);
                strcpy(cat[cnt_cat].b, b);
                cnt_cat++;                        //统计喜欢猫人数
            }
            else
            {
                strcpy(dog[cnt_dog].a, a);
                strcpy(dog[cnt_dog].b, b);
                cnt_dog++;
            }       
        }

        memset(maze, false, sizeof(maze));
        for (int i = 0; i < cnt_cat; i++)
            for (int j = 0; j < cnt_dog; j++)        
                if (strcmp(cat[i].a, dog[j].b) == 0 || strcmp(cat[i].b, dog[j].a) == 0)             
                    maze[i][j] = true;

        int ans = 0;
        memset(match, -1, sizeof(match));
        for (int i = 0; i < cnt_cat; i++)
        {
            memset(isvisit, false, sizeof(isvisit));
            if (find(i))
                ans++;
        }

        printf("%d\n", v-ans);
    }
    return 0;
}

你可能感兴趣的:(ACM,HDU,最大独立集,匈牙利算法,最小点覆盖)