Cat vs. Dog
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1602 accepted submission (s): 606
Problem descriptionthe latest reality show has hit the TV: ''cat. 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 shocould stay and which shocould be forced to leave the show.
Each viewer gets to cast a vote on two things: one pet which shocould be kept on the show, and one pet which shocould 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 your 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.
Inputon 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.
Outputper testcase:
* One Line with the maximum possible number of satisfied voters for the show.
Sample input21 1 2C1 d1d1 C11 2 4C1 d1c1 d1c1 d2d2 C1
Sample output13
Source nwerc 2008 Algorithm: separates people who like cats and dogs to form a bipartite graph. In this way, people who like cats and dogs will not conflict with each other, and then scan them once, connect conflicting people to one side. The biggest independent set of a bipartite graph is the answer. According to the known theorem, the maximum independent set of a bipartite graph = the number of vertices-the maximum matching of a bipartite graph. Obtain the maximum matching value of the Bipartite Graph.
1 const int MAXN = 510; 2 int uN,vN;//u,v的数目,使用前面必须赋值 3 int g[MAXN][MAXN];//邻接矩阵 4 int linker[MAXN]; 5 bool used[MAXN]; 6 #include <memory.h> 7 bool dfs(int u) 8 { 9 for(int v = 0; v < vN; v++)10 if(g[u][v] && !used[v])11 {12 used[v] = true;13 if(linker[v] == -1 || dfs(linker[v]))14 {15 linker[v] = u;16 return true;17 }18 }19 return false;20 }21 int hungary()22 {23 int res = 0;24 memset(linker,-1,sizeof(linker));25 for(int u = 0; u < uN; u++)26 {27 memset(used,false,sizeof(used));28 if(dfs(u))res++;29 }30 return res;31 }32 33 #include <iostream>34 #include <stdio.h>35 #include <string.h>36 using namespace std;37 38 39 int getint(char s[])40 {41 int ans=0;42 for(int i=0; i<strlen(s); i++)43 {44 ans=ans*10+s[i]-‘0‘;45 }46 return ans;47 }48 49 typedef pair<int,int> CPair;50 51 CPair upair[510],vpair[510];52 int main()53 {54 #ifndef ONLINE_JUDGE55 freopen("in.txt","r",stdin);56 #endif // ONLINE_JUDGE57 int T;58 cin>>T;59 while(T--)60 {61 memset(g,0,sizeof g);62 int nc,nd,n;63 uN=vN=0;64 scanf("%d%d%d",&nc,&nd,&n);65 for(int i=0; i<n; i++) // 人的数量66 {67 char s1[20],s2[20];68 scanf("%s%s",s1,s2); // C1 D169 int ci=getint(s1+1),di=getint(s2+1);70 if(s1[0]==‘C‘)71 {72 // 放到第一个集合73 upair[uN++]=CPair(ci,di);74 }75 else76 {77 vpair[vN++]=CPair(ci,di);78 }79 }80 // 构图81 for(int i=0;i<uN;i++)82 for(int j=0;j<vN;j++)83 {84 // 矛盾的话就连一条边85 if(upair[i].first==vpair[j].second || upair[i].second==vpair[j].first)86 g[i][j]=1;87 }88 89 int ans=uN+vN-hungary();90 printf("%d\n",ans);91 }92 return 0;93 }
Hdu2768-Cat vs. Dog: Graph Theory: binary matching