Cat vs. Dog
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2279 Accepted Submission(s): 886
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
思路:
以cat_lover和dog_lover把观众分为两个集合。只要两个集合内的人的选择有冲突,这两个顶点连接,边代表矛盾,然后求最大独立集。
最大独立集 = 顶点数 - 最小顶点覆盖数(最大匹配数)
题目中告诉我们喜欢狗的一定不喜欢猫,喜欢猫的人一定不喜欢狗,那么这样我们就把观众分为了两类,一类是喜欢猫的,另一类是喜欢狗的,我们知道这两种观众不可能同时满足,这个时候我们要满足最多的人,这个时候就转化成了最大独立集。
这个时候我们构的图为以喜欢的动物为一个集合,然后将喜欢的动物与讨厌的动物相同(即发生矛盾)的人连线,然后跑二分图匹配。
代码:
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define N 510 using namespace std; char ch1,ch2; bool vis[N]; int t,x,y,n,m,k,ans,sdog,scat,girl[N],map[N][N]; struct Cat { int like,hate; }cat[N]; struct Dog { int like,hate; }dog[N]; int find(int x) { for(int i=1;i<=sdog;i++) { if(!vis[i]&&map[x][i]) { vis[i]=true; if(girl[i]==-1||find(girl[i])) {girl[i]=x; return 1;} } } return 0; } int main() { scanf("%d",&t); while(t--) { scanf("%d%d%d",&n,&m,&k); ans=0;sdog=0,scat=0; memset(map,0,sizeof(map)); for(int i=1;i<=k;i++) { getchar();//读入换行 scanf("%c%d",&ch1,&x); getchar();//读入空格 scanf("%c%d",&ch2,&y); if(ch1==‘C‘) { cat[++scat].like=x; cat[scat].hate=y; } else { dog[++sdog].like=x; dog[sdog].hate=y; } } for(int i=1;i<=scat;i++) for(int j=1;j<=sdog;j++) if(cat[i].like==dog[j].hate||cat[i].hate==dog[j].like) map[i][j]=1; memset(girl,-1,sizeof(girl)); for(int i=1;i<=scat;i++) { memset(vis,0,sizeof(vis)); if(find(i)) ans++; } printf("%d\n",k-ans); } return 0; }