HDU 3829 - Cat VS Dog【二分图最大匹配最大独立集】

Submit Status

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 2 C1 D1 D1 C1 1 2 4 C1 D1 C1 D1 C1 D2 D2 C1
 

Sample Output

      
      
      
      
1 3

Hint

Case 2: Remove D1 and D2, that makes child 1, 2, 3 happy. 
         
 

瞅着眼熟,和暑假做过的

HDU 2768Cat vs. Dog

除了叙述不一样,其他都一样,之前纠结于如果是把猫放左边,狗放右边,那喜欢狗不喜欢猫的怎么办?灵光乍一闪想到之前的题。

题意说如果小孩喜欢的动物没拿掉,不喜欢的拿掉了,那么happy值++。我们换个想法,如果存在了两个小孩的喜恶相反,才会出现只能满足一方要求的情况,我们用小孩的喜恶来建边,而不要纠结于猫还是狗,矛盾了就建边。最大独立集=点数-最大匹配

#include <iostream>
#include <stdio.h>
#include <string.h>
#define M 550
#define inf 0x3f3f3f3f
using namespace std;
const int MAXN=550;
int uN,vN;//u,v数目
int g[MAXN][MAXN];
int linker[MAXN];
bool used[MAXN];
bool dfs(int u)//从左边开始找增广路径
{
    int v;
    for(v=0;v<vN;v++)//这个顶点编号从0开始,若要从1开始需要修改
      if(g[u][v]&&!used[v])
      {
          used[v]=true;
          if(linker[v]==-1||dfs(linker[v]))
          {//找增广路,反向
              linker[v]=u;
              return true;
          }
      }
    return false;//这个不要忘了,经常忘记这句
}
int hungary()
{
    int res=0;
    int u;
    memset(linker,-1,sizeof(linker));
    for(u=0;u<uN;u++)
    {
        memset(used,0,sizeof(used));
        if(dfs(u)) res++;
    }
    return res;
}
char like[550][10],dislike[550][10];
int main()
{
  //  freopen("cin.txt","r",stdin);
    int c,d,v;
    while(~scanf("%d%d%d",&c,&d,&v))
    {
            vN=uN=v;
            for(int i=0;i<v;i++) scanf("%s%s",like[i],dislike[i]);
            memset(g,0,sizeof(g));
            for(int i=0;i<v;i++)
                for(int j=0;j<v;j++)
                {
                    if(strcmp(like[i],dislike[j])==0||strcmp(like[j],dislike[i])==0)
                    g[i][j]=1;
                }
            printf("%d\n",v-hungary()/2);
    }
    return 0;
}


你可能感兴趣的:(HDU 3829 - Cat VS Dog【二分图最大匹配最大独立集】)