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): 967    Accepted Submission(s): 356


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

转:

当观众想要留下来的宠物出局时,这个观众才认为会离开 。

 以cat_lover和dog_lover把观众分为两个集合。只要两个集合内的人的选择有冲突,这两个顶点连接,边代表矛盾,然后求最大独立集。

  最大独立集 = 顶点数 - 最小顶点覆盖数(最大匹配数)

 代码:
View Code
 1 #include<iostream>

 2 using namespace std;

 3  

 4 const int N = 505;

 5  

 6 struct option

 7 {

 8     char a[5], b[5];        //a表示喜欢的动物。b表示不喜欢的动物

 9 };

10  

11 int maze[N][N];

12 option cat[N], dog[N];

13 int isvisit[N];

14 int match[N];

15 int c, d, v;

16 int cnt_cat, cnt_dog;

17  

18 bool find (int u)                        //匈牙利算法

19 {

20     for (int i = 0; i < cnt_dog; i++)

21         if (maze[u][i] && !isvisit[i])

22         {

23             isvisit[i] = true;

24             if (match[i] == -1 || find(match[i]))

25             {

26                 match[i] = u;

27                 return true;

28             }

29         }

30  

31     return false;

32 }

33  

34 int main()

35 {

36     int cases;

37     char a[5];

38     char b[5];

39  

40     scanf("%d", &cases);

41     while (cases--)

42     {

43         scanf("%d%d%d", &c, &d, &v);

44         cnt_cat = 0;

45         cnt_dog = 0;

46         for (int i = 0; i < v; i++)  

47         {

48             scanf("%s%s", a, b);

49      

50             if (a[0] == 'C')

51             {

52                 strcpy(cat[cnt_cat].a, a);

53                 strcpy(cat[cnt_cat].b, b);

54                 cnt_cat++;                        //统计喜欢猫人数

55             }

56             else

57             {

58                 strcpy(dog[cnt_dog].a, a);

59                 strcpy(dog[cnt_dog].b, b);

60                 cnt_dog++;

61             }       

62         }

63  

64         memset(maze, false, sizeof(maze));

65         for (int i = 0; i < cnt_cat; i++)

66             for (int j = 0; j < cnt_dog; j++)        

67                 if (strcmp(cat[i].a, dog[j].b) == 0 || strcmp(cat[i].b, dog[j].a) == 0)             

68                     maze[i][j] = true;

69                  

70         int ans = 0;

71         memset(match, -1, sizeof(match));

72         for (int i = 0; i < cnt_cat; i++)

73         {

74             memset(isvisit, false, sizeof(isvisit));

75             if (find(i))

76                 ans++;

77         }

78  

79         printf("%d\n", v-ans);

80     }

81     return 0;

82 }

 

你可能感兴趣的:(HDU)