Total Submission(s): 2561 Accepted Submission(s): 875
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.
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)
#include<algorithm>
#include<stdio.h>
#include<queue>
#include<string>
#include<iostream>
#include<string.h>
#include<vector>
#include<map>
using namespace std;
int use[5005];
int r[5005];
vector<int> a[5005];
int n,m,p;
bool match(int now)
{
int len=a[now].size();
for(int i=0;i<len;i++)
{
if(use[a[now][i]]==1)
continue;
use[a[now][i]]=1;
if(r[a[now][i]]==-1||match(r[a[now][i]]))
{
r[a[now][i]]=now;
return true;
}
}
return false;
}
int solve()
{
int ans=0;
for(int i=1;i<=p;i++)
{
memset(use,0,sizeof(use)); //初始化
if(match(i))
ans++;
}
return ans;
}
int main()
{
while(scanf("%d%d%d",&n,&m,&p)!=EOF)
{
for(int i=0;i<=5000;i++)
a[i].clear();
memset(r,-1,sizeof(r));
map<int,string>like;
map<int,string>notlike;
string c1,c2;
for(int i=1;i<=p;i++)
{
cin>>c1>>c2;
like[i]=c1;
notlike[i]=c2;
}
for(int i=1;i<p;i++)
{
for(int j=i+1;j<=p;j++)
{
if(like[i]==notlike[j]||notlike[i]==like[j])
{
a[i].push_back(j);
a[j].push_back(i);
}
}
}
int ans=solve();
printf("%d\n",p-ans/2);
}
return 0;
}