1. 程式人生 > >HDU3829 Cat VS Dog —— 最大獨立集

HDU3829 Cat VS Dog —— 最大獨立集

ref key 之間 ring dog 沖突 lib ssi iss

題目鏈接: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 2011 Multi-University Training Contest 1 - Host by HNU

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 —— 最大獨立集