Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2030 Accepted Submission(s): 774
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.
* 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.
* One line with the maximum possible number of satisfied voters for the show.
#include<iostream> #include<cstdio> #include<cstring> #include <algorithm> #include <math.h> #include <queue> using namespace std; const int N = 505; int c,d,v; char a[N][10],b[N][10]; int graph[N][N]; int linker[N]; bool vis[N]; bool dfs(int u){ for(int i=1;i<=v;i++){ if(graph[u][i]&&!vis[i]){ vis[i] = true; if(linker[i]==-1||dfs(linker[i])){ linker[i] = u; return true; } } } return false; } int main() { int tcase; scanf("%d",&tcase); while(tcase--){ scanf("%d%d%d",&c,&d,&v); for(int i=1;i<=v;i++){ scanf("%s%s",a[i],b[i]); } memset(graph,0,sizeof(graph)); for(int i=1;i<=v;i++){ for(int j=1;j<=v;j++){ if(strcmp(a[i],b[j])==0||strcmp(a[j],b[i])==0){ graph[i][j] = graph[j][i] = 1; } } } int res = 0; memset(linker,-1,sizeof(linker)); for(int i=1;i<=v;i++){ memset(vis,false,sizeof(vis)); if(dfs(i)) res++; } printf("%d\n",v-res/2); } return 0; }