HDU3829 Cat VS Dog —— 最大独立集

Posted h_z_cong

tags:

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

题目链接:https://vjudge.net/problem/HDU-3829

 

Cat VS Dog

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)
Total Submission(s): 4118    Accepted Submission(s): 1493


Problem Description
The zoo have N cats and M dogs, today there are P children visiting the zoo, each child has a like-animal and a dislike-animal, if the child‘s like-animal is a cat, then his/hers dislike-animal must be a dog, and vice versa.
Now the zoo administrator is removing some animals, if one child‘s like-animal is not removed and his/hers dislike-animal is removed, he/she will be happy. So the administrator wants to know which animals he should remove to make maximum number of happy children.
 

 

Input
The input file contains multiple test cases, for each case, the first line contains three integers N <= 100, M <= 100 and P <= 500.
Next P lines, each line contains a child‘s like-animal and dislike-animal, C for cat and D for dog. (See sample for details)
 

 

Output
For each case, output a single integer: the maximum number of happy children.
 

 

Sample Input
1 1 2 C1 D1 D1 C1 1 2 4 C1 D1 C1 D1 C1 D2 D2 C1
 

 

Sample Output
1 3
Hint
Case 2: Remove D1 and D2, that makes child 1, 2, 3 happy.
 

 

Source
 

 

Recommend
xubiao

 

 

题解:

1.如果小孩u喜欢的与小孩v讨厌的相同,或者小孩u讨厌的与小孩v喜欢的相同,则表明他们两个人有冲突。在u和v之间连一条边。

2.利用匈牙利算法,求出最大匹配数cnt,即为最小点覆盖。答案就为 n - cnt 。为何?

答:所谓最小点覆盖,即用最少的点,去覆盖掉所有的边。如果我们把这最小覆盖点集都删除,那么图中就不存在边了,也就是不存在冲突,剩下的人可以和平共处了。又因为是“最小”点覆盖, 所以删除的点是最少的,所以留下来的点是最多的。

 

 

代码如下:

技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <string>
 6 #include <vector>
 7 #include <map>
 8 #include <set>
 9 #include <queue>
10 #include <sstream>
11 #include <algorithm>
12 using namespace std;
13 const int INF = 2e9;
14 const int MOD = 1e9+7;
15 const int MAXN = 500+10;
16 
17 int n, m, p;
18 int M[MAXN][MAXN], link[MAXN];
19 bool vis[MAXN];
20 char like[MAXN][5], hate[MAXN][5];
21 
22 bool dfs(int u)
23 {
24     for(int i = 1; i<=p; i++)
25     if(M[u][i] && !vis[i])
26     {
27         vis[i] = true;
28         if(link[i]==-1 || dfs(link[i]))
29         {
30             link[i] = u;
31             return true;
32         }
33     }
34     return false;
35 }
36 
37 int hungary()
38 {
39     int ret = 0;
40     memset(link, -1, sizeof(link));
41     for(int i = 1; i<=p; i++)
42     {
43         memset(vis, 0, sizeof(vis));
44         if(dfs(i)) ret++;
45     }
46     return ret;
47 }
48 
49 int main()
50 {
51     while(scanf("%d%d%d", &n, &m, &p)!=EOF)
52     {
53         for(int i = 1; i<=p; i++)
54             scanf("%s%s", like[i], hate[i]);
55 
56         memset(M, false, sizeof(M));
57         for(int i = 1; i<=p; i++)
58         for(int j = 1; j<=p; j++)
59             if(!strcmp(like[i], hate[j]) || !strcmp(hate[i], like[j]))
60                 M[i][j] = true;
61 
62         int cnt = hungary()/2;
63         printf("%d\n", p-cnt);
64     }
65 }
View Code

 







以上是关于HDU3829 Cat VS Dog —— 最大独立集的主要内容,如果未能解决你的问题,请参考以下文章

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

hdu 3829 Cat VS Dog 二分图匹配 最大点独立集

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

hdu 3829 Cat VS Dog 二分匹配 最大独立点集

Cat VS Dog HDU_3829(最大独立集最大匹配)

HDU3829 Cat VS Dog