hdu3829

来源:互联网 发布:配置php开发环境 编辑:程序博客网 时间:2024/05/29 09:46
B - Cat VS Dog
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 3829

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

1

3

有C个猫咪,D只汪,P个孩子,每个孩子要么喜欢猫,要么喜欢狗,如果把小孩A不喜欢的动物拿走,那么他就会开心,尝试拿走一些动物,使最多的小孩子开心,输出最多几人开心

将喜欢的和不喜欢的放在两列,如果一动物既有人喜欢,又有人讨厌,那么这就是一组矛盾,如果求出最少的矛盾对数,岂不是找到了最大开心人数,最大独立集=定点数-最大匹配数,就是独立集内的人和那些矛盾集没有关系,哎怎么矛盾怎么矛盾

#include <iostream>#include <map>#include <string.h>#include <stdio.h>#include <string>#include <math.h>using namespace std;const int maxn=1010;const int inf=0x3f3f3f3f;int C,D,P;int g[maxn][maxn];int linker[maxn];bool used[maxn];int n;bool dfs(int u){    for(int i=1; i<=n; i++)    {        if(!used[i]&&g[u][i])        {            used[i]=true;            if(!linker[i]||dfs(linker[i]))            {                linker[i]=u;                return true;            }        }    }    return false;}int solve(){    int ans=0;    memset(linker,0,sizeof(linker));    for(int i=1; i<=n; i++)    {        memset(used,false,sizeof(used));        if(dfs(i))            ans++;    }    return ans;} char str1[510][10],str2[510][10];int main(){    while(~scanf("%d%d%d",&C,&D,&P))    {        n=P;        memset(g,0,sizeof(g));        for(int i=1;i<=P;i++)         cin>>str1[i]>>str2[i];        for(int i=1;i<=P;i++)        {            for(int j=1;j<=P;j++)            {                if(strcmp(str1[i],str2[j])==0)                   g[j][i]=g[i][j]=1;            }        }        printf("%d\n",P-solve()/2);    }    return 0;}


0 0