貌似这个是ACM的题目。。。。
Cat vs Dog
--------------------------------------------------------------------------------
Time limit: 2sec. Submitted: 43
Memory limit: 64M Accepted: 14
Source: NWERC 2008
--------------------------------------------------------------------------------
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
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
下面是我AC的代码如下:
#include<cstdio>
#include<cstring>
#include<vector>
#define MAXN 2100
using namespace std;
struct NODE{
int x,y;
}arr[MAXN];
bool mark[MAXN],root[MAXN];
int pre[MAXN],c,d,n;
vector<int>v[MAXN];
int T(char ch,int x){
if(ch=='C')return x;
else return c+x;
}
bool DFS(int x){
for(int i=0;i<v[x].size();i++){
int t=v[x][i];
if(!mark[t]){
mark[t]=true;
if(pre[t]==-1||DFS(pre[t])){
pre[t]=x;return true;
}
}
}
return false;
}
int Max_Match(){
int ans=0;
memset(pre,0xff,sizeof(pre));
memset(root,0,sizeof(root));
for(int i=0;i<n;i++){
memset(mark,0,sizeof(mark));
if(DFS(i))ans++;
}
return n-ans/2;
}
int main(){
char ch,ach;
int cases,i,j,a,b;
scanf("%d",&cases);
while(cases--){
scanf("%d %d %d",&c,&d,&n);
for(i=0;i<MAXN;i++)v[i].clear();
for(i=0;i<n;i++){
getchar();
scanf("%c%d %c%d",&ch,&a,&ach,&b);
arr[i].x=T(ch,a);
arr[i].y=T(ach,b);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(arr[i].x==arr[j].y||arr[i].y==arr[j].x)
v[i].push_back(j);
printf("%d\n",Max_Match());
}
}