码迷,mamicode.com
首页 > 其他好文 > 详细

HDU3829 Cat VS Dog —— 最大独立集

时间:2017-11-11 13:05:03      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:string   pen   匈牙利算法   put   小孩   inpu   mit   nbsp   ane   

题目链接: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 —— 最大独立集

标签:string   pen   匈牙利算法   put   小孩   inpu   mit   nbsp   ane   

原文地址:http://www.cnblogs.com/DOLFAMINGO/p/7818522.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!