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
题意:c只猫,v条狗,v个人,现在每个人都会选一只喜欢的宠物和一只不喜欢的宠物,问如何让满意的人最多
思路:这题难在建边,只要尽量把好有冲突的人选在一起就好,把喜好有冲突的人连边,然后求最大独立集,由于实际上进行了拆点,最大独立集等于v-最大匹配/2;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#include<queue>
using namespace std;
#define maxn 305
#define ll long long
int c,d,v,id=0;
struct Edge
{
int to,next;
}edge[maxn];
int line[maxn],head[maxn],vis[maxn],cnt,like[maxn],unlike[maxn];
void addedge(int u,int v)
{
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
memset(line,-1,sizeof(line));
}
string a,b;
int dfs(int u)
{
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(vis[v]==0)
{
vis[v]=1;
if(line[v]==-1||dfs(line[v])==1)
{
line[v]=u;
return 1;
}
}
}
return 0;
}
int slove()
{
int ans=0;
for(int i=1;i<=v;i++)
{
memset(vis,0,sizeof(vis));
ans+=dfs(i);
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
map<string,int>q;
id=0;
scanf("%d %d %d",&c,&d,&v);
for(int i=1;i<=v;i++)
{
cin>>a>>b;
if(q.count(a)==0)
{
q[a]=++id;
}
like[i]=q[a];
if(q.count(b)==0)
{
q[b]=++id;
}
unlike[i]=q[b];
}
for(int i=1;i<=v;i++)
{
for(int j=1;j<=v;j++)
{
if(unlike[i]==like[j]||like[i]==unlike[j])
addedge(i,j);
}
}
int ans=0;
ans=slove();
printf("%d\n",v-ans/2);
}
}