HDOJ 3829 Cat VS Dog

来源:互联网 发布:php 16进制转字符串 编辑:程序博客网 时间:2024/06/05 04:32
Cat VS DogTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)
Total Submission(s): 2561    Accepted Submission(s): 875


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.
 

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)
 

Output
For each case, output a single integer: the maximum number of happy children.
 

Sample Input
1 1 2C1 D1D1 C11 2 4C1 D1C1 D1C1 D2D2 C1
 

Sample Output
13
Hint
Case 2: Remove D1 and D2, that makes child 1, 2, 3 happy.
 

Source
2011 Multi-University Training Contest 1 - Host by HNU
 
题意:动物园里面只有猫和狗,对于每个人有喜欢的动物和不喜欢的动物,现在要删除一些动物,让最多的人满意,也就是最多的人喜欢的动物保留下来并且不喜欢的动物被删除。
直接来看结论,当达到最多的人的时候,留下来的人,没有矛盾,也就是没有人喜欢的动物是其他人不喜欢的动物,其余的人都是和这些人有矛盾的人,将这些人按照矛盾来划分为不同的集合,所有这里求的就是最大的集合,也就是图论中的最大独立集合,我们按照矛盾将这些人建边,如果两个人有矛盾,我们就在这两个人之间连边,最后求出最大的二分匹配,然后用人数剪出匹配的人数,剩下的就是答案。

#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;}

0 0