Cat VS Dog
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)Total Submission(s): 3777 Accepted Submission(s): 1352
Problem Description
The zoo have N cats and M dogs, today there are P children visiting the zoo, each child has a like-animal and a dislike-animal, if the child's like-animal is a cat, then his/hers dislike-animal must be a dog, and vice versa.
Now the zoo administrator is removing some animals, if one child's like-animal is not removed and his/hers dislike-animal is removed, he/she will be happy. So the administrator wants to know which animals he should remove to make maximum number of happy children.
Now the zoo administrator is removing some animals, if one child's like-animal is not removed and his/hers dislike-animal is removed, he/she will be happy. So the administrator wants to know which animals he should remove to make maximum number of happy children.
Input
The input file contains multiple test cases, for each case, the first line contains three integers N <= 100, M <= 100 and P <= 500.
Next P lines, each line contains a child's like-animal and dislike-animal, C for cat and D for dog. (See sample for details)
Next P lines, each line contains a child's like-animal and dislike-animal, C for cat and D for dog. (See sample for details)
Output
For each case, output a single integer: the maximum number of happy children.
Sample Input
1 1 2 C1 D1 D1 C1 1 2 4 C1 D1 C1 D1 C1 D2 D2 C1
Sample Output
1 3HintCase 2: Remove D1 and D2, that makes child 1, 2, 3 happy.
Source
题意:p个小朋友参观动物园,动物园里面有两种动物,分别为猫和狗。一个小朋友喜欢猫就讨厌狗,喜欢狗就讨厌猫。现在要移走一些动物。如果一个小朋友喜欢猫3,讨厌狗4.那么移走狗4,这个小朋友就会非常开心。同样,如果移走猫3,小朋友就会很不高兴。现在问怎么样才能使开心的小朋友的人数最多。
解题思路:把喜欢猫不喜欢狗的和喜欢狗的不喜欢猫的分到两个集合,如果两个人其中一人喜欢的恰恰是另一个人不喜欢的,连一条边证明两人的喜好是矛盾的,然后就是最大独立集的问题了,最大独立集的点数=顶点数-最大匹配数
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
#include <bitset>
using namespace std;
#define LL long long
const int INF=0x3f3f3f3f;
int n,m,p;
int xn,yn;
int x[505],y[505];
int visit[505];
int mp[505][505];
string s1,s2;
string a[505][2],b[505][2];
bool path(int k)
{
for(int i=1;i<yn;i++)
{
if(!visit[i]&&mp[k][i])
{
visit[i]=1;
if(y[i]==-1||path(y[i]))
{
y[i]=k;
x[k]=i;
return 1;
}
}
}
return 0;
}
void MaxMatch()
{
int ans=0;
memset(x,-1,sizeof x);
memset(y,-1,sizeof y);
for(int i=1;i<xn;i++)
{
if(x[i]==-1)
{
memset(visit,0,sizeof visit);
if(path(i)) ans++;
}
}
printf("%d\n",p-ans);
}
int main()
{
while(~scanf("%d %d %d",&n,&m,&p))
{
memset(mp,0,sizeof mp);
xn=yn=1;
for(int i=1;i<=p;i++)
{
cin>>s1>>s2;
if(s1[0]=='C')
{
a[xn][0]=s1;
a[xn++][1]=s2;
}
else
{
b[yn][0]=s1;
b[yn++][1]=s2;
}
}
for(int i=1;i<xn;i++)
{
for(int j=1;j<yn;j++)
{
if(a[i][0]==b[j][1]||a[i][1]==b[j][0])
mp[i][j]=1;
}
}
MaxMatch();
}
return 0;
}