Fellow me on GitHub

HDU3829(KB10-J 二分图最大独立集)

Cat VS Dog

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


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

 
在有矛盾的男孩之间连边,有矛盾定义为两种情况:
  1.我喜欢的你不喜欢
  2.我不喜欢的你喜欢
建图后,求最大的两两互不相连的顶点集合即为答案,即求图的最大独立集。
一般图的最大独立集难求,转换为二分图,对男孩进行拆点,为i和p+i。若i和j有矛盾,则i与p+j、j与p+i连边。记得匹配数要除2。
 
定理:二分图最大独立集 == 顶点数 - 最小顶点覆盖 == 顶点数 - 二分图最大匹配
 1 //2017-08-25
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 
 7 using namespace std;
 8 
 9 const int N = 2000;
10 const int M = 500000;
11 int head[N], tot;
12 struct Edge{
13     int to, next;
14 }edge[M];
15 
16 void init(){
17     tot = 0;
18     memset(head, -1, sizeof(head));
19 }
20 
21 void add_edge(int u, int v){
22     edge[tot].to = v;
23     edge[tot].next = head[u];
24     head[u] = tot++;
25 
26     edge[tot].to = u;
27     edge[tot].next = head[v];
28     head[v] = tot++;
29 }
30 
31 int n, m, p;
32 string G[N];
33 int matching[N];
34 int check[N];
35 
36 bool dfs(int u){
37     for(int i =  head[u]; i != -1; i = edge[i].next){
38         int v = edge[i].to;
39         if(!check[v]){//要求不在交替路
40             check[v] = 1;//放入交替路
41             if(matching[v] == -1 || dfs(matching[v])){
42                 //如果是未匹配点,说明交替路为增广路,则交换路径,并返回成功
43                 matching[u] = v;
44                 matching[v] = u;
45                 return true;
46             }
47         }
48     }
49     return false;//不存在增广路
50 }
51 
52 //hungarian: 二分图最大匹配匈牙利算法
53 //input: null
54 //output: ans 最大匹配数
55 int hungarian(){
56     int ans = 0;
57     memset(matching, -1, sizeof(matching));
58     for(int u = 1; u <= p; u++){
59         if(matching[u] == -1){
60             memset(check, 0, sizeof(check));
61             if(dfs(u))
62                   ans++;
63         }
64     }
65     return ans;
66 }
67 
68 string like[N], dislike[N];
69 
70 int main()
71 {
72     std::ios::sync_with_stdio(false);
73     //freopen("inputJ.txt", "r", stdin);
74     while(cin>>n>>m>>p && n){
75         init();
76         for(int i = 1; i <= p; i++)
77               cin>>like[i]>>dislike[i];
78         for(int i = 2; i <= p; i++){
79             for(int j = 1; j < i; j++){
80                 if(like[i] == dislike[j] || dislike[i] == like[j]){
81                     add_edge(i, p+j);
82                     add_edge(j, p+i);
83                 }
84             }
85         }
86         cout<<p-hungarian()/2<<endl;
87     }
88 
89     return 0;
90 }

 

posted @ 2017-08-25 22:45  Penn000  阅读(224)  评论(0编辑  收藏  举报