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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章