2014
02-14

# Cat vs. Dog

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.

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.

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.

2
1 1 2
C1 D1
D1 C1
1 2 4
C1 D1
C1 D1
C1 D2
D2 C1

1
3

http://acm.hdu.edu.cn/showproblem.php?pid=2768

#include <iostream>
#include <cstring>
#include <cstdio>
#define INF 0x3f3f3f3f
#define BUG printf("here!\n")
using namespace std;
const int MAXN=505;
int map[MAXN][MAXN];
int vis[MAXN];
bool dfs(int u,int n)
{
int v;
for(v=1;v<=n;v++)
{
if(!vis[v]&&map[u][v])
{
vis[v]=1;
{
return true;
}
}
}
return false;
}
int match(int n,int m)
{
int sum=0;
int i;
for(i=1;i<=n;i++)
{
memset(vis,0,sizeof(vis));
if(dfs(i,m))
sum++;
}
return sum;
}
int like[505],dislike[505];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int c,d,m;
scanf("%d%d%d",&c,&d,&m);
int i;
getchar();
char a1,a2;
int b1,b2;
for(i=1;i<=m;i++)
{
scanf("%c%d %c%d",&a1,&b1,&a2,&b2);
getchar();
if(a1=='D')
like[i]=1*1000+b1;
else
like[i]=2*1000+b1;

if(a2=='D')
dislike[i]=1*1000+b2;
else
dislike[i]=2*1000+b2;
}
memset(map,0,sizeof(map));
int j;
for(i=1;i<=m;i++)
{
for(j=i+1;j<=m;j++)
{
if(like[i]==dislike[j])
map[i][j]=map[j][i]=1;
if(dislike[i]==like[j])
map[i][j]=map[j][i]=1;
}
}
int res=match(m,m);
printf("%d\n",m-res/2);
}

return 0;
}

1. 有限自动机在ACM中是必须掌握的算法，实际上在面试当中几乎不可能让你单独的去实现这个算法，如果有题目要用到有限自动机来降低时间复杂度，那么这种面试题应该属于很难的级别了。

2. 有两个重复的话结果是正确的，但解法不够严谨，后面重复的覆盖掉前面的，由于题目数据限制也比较严，所以能提交通过。已更新算法