Cat vs. Dog |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission(s): 164 Accepted Submission(s): 63 |
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
NWERC 2008
|
Recommend
lcy
|
分析:以投票者为结点,将有矛盾的人之间连线,找最大独立集=节点数-最大匹配数
#include<cstdio> #include<cstring> int match[510],vis[510],map[510][510]; int n; int dfs(int a){ int i; for(i=1;i<=n;++i){ if(!map[a][i] || vis[i]) continue; vis[i]=1; if(match[i]==-1 || dfs(match[i])){ match[i]=a; return 1; } } return 0; } char pet[510][2][8]; int main(){ int c,d,i,j,T,ans; scanf("%d",&T); while(T--){ memset(map,0,sizeof(map)); memset(match,-1,sizeof(match)); scanf("%d%d%d",&c,&d,&n); for(i=1;i<=n;++i){ scanf("%s%s",pet[i][0],pet[i][1]); } for(i=2;i<=n;++i){ for(j=1;j<i;++j){ if(strcmp(pet[i][0],pet[j][1])==0 || strcmp(pet[i][1],pet[j][0])==0){ map[i][j]=map[j][i]=1; } } } ans=0; for(i=1;i<=n;++i){ memset(vis,0,sizeof(vis)); if(dfs(i)) ++ans; } printf("%d\n",n-ans/2); } return 0; }