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

hdu 2768 Cat vs. Dog (二分匹配)

时间:2014-05-30 03:30:21      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   class   blog   code   

Cat vs. Dog

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1422    Accepted Submission(s): 534


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
 

 

Source
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:  2767 2769 2772 2770 2773 
 

 好好地一题最大独立集被我毁了...

开始思路匹配错了.后来才想清楚,人应该和人匹配,构图原则是一个人喜欢的和另一个人不喜欢的一样则两者匹配,最后求最大独立集。

最大独立集=原点数-最大匹配/2(构图时构双向图);

bubuko.com,布布扣
 1 //93MS    1780K    1160 B    C++
 2 #include<iostream>
 3 #include<vector>
 4 #include<string.h>
 5 #define N 505
 6 using namespace std;
 7 int match[N];
 8 int vis[N];
 9 int n,m,k;
10 vector<int>V[N];
11 int dfs(int u)
12 {
13     for(int i=0;i<V[u].size();i++){
14         int v=V[u][i];
15         if(!vis[v]){
16             vis[v]=1;
17             if(match[v]==-1 || dfs(match[v])){
18                 match[v]=u;
19                 return 1;
20             }
21         }
22     }
23     return 0;
24 }
25 int hungary()
26 {
27     int ret=0;
28     memset(match,-1,sizeof(match));
29     for(int i=0;i<k;i++){
30         memset(vis,0,sizeof(vis));
31         ret+=dfs(i);
32     }
33     return ret;
34 }
35 int main(void)
36 {
37     char a[N][5],b[N][5];
38     int t;
39     scanf("%d",&t);
40     while(t--)
41     {
42         scanf("%d%d%d",&n,&m,&k);
43         for(int i=0;i<=k;i++) V[i].clear();
44         for(int i=0;i<k;i++)
45             scanf("%s%s",&a[i],&b[i]);
46         for(int i=0;i<k;i++)
47             for(int j=0;j<k;j++){
48             if(strcmp(a[i],b[j])==0 || strcmp(a[j],b[i])==0){
49                  V[i].push_back(j);
50                  V[j].push_back(i);
51             }
52         }
53         printf("%d\n",k-hungary()/2);
54     }
55     return 0;
56 }
bubuko.com,布布扣

 

 

 

hdu 2768 Cat vs. Dog (二分匹配),布布扣,bubuko.com

hdu 2768 Cat vs. Dog (二分匹配)

标签:des   c   style   class   blog   code   

原文地址:http://www.cnblogs.com/GO-NO-1/p/3757721.html

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