HDU 3829 Cat VS Dog(二分图最大独立集)

题意:动物园有N只猫,M只狗,P个小孩。每个小孩都有自己喜欢的动物和讨厌的动物,如果他喜欢狗,那么就讨厌猫,如果他讨厌猫,那么他就喜欢狗。某个小孩能开心,当且仅当他喜欢的动物留在动物园和讨厌的动物不在动物园里面。现让管理员通过带走某些动物,问最多能使多少个孩子开心。

思路:正难则反的建图,我们把所有喜欢狗的孩子放在左边的点集中,所有喜欢猫的孩子放在右边的点集中.如果左边的小孩i喜欢的狗与右边的小孩j讨厌的狗是同一只的话,那么在左i与右j之间就连一条无向边.如果左边的小孩i讨厌的猫与右边的小孩j喜欢的猫是同一只的话,那么在左i与右j之间就连一条无向边.现在管理员要让尽量多的小孩高兴,明显管理员必须想办法选出尽量多的小孩出来,把他们讨厌的动物都抛弃且把他们喜欢的东西都留下才行.注意这些被选出的小孩必须满足:任意两个小孩之间不存在边.因为如果有两个小孩之间存在边,那么说明他们之间有一只动物是一个人喜欢的,另一个人讨厌的.管理员不可能同时满足他们两.

          所以现在的问题是:管理员如果在新建的二分图中选出尽量多的点来,使得任意两点之间不存在边. 即最大独立集=n-最大匹配数.


#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=500+10;

struct Max_Match
{
    int n,m;
    int g[maxn][maxn];
    bool vis[maxn];
    int left[maxn];

    void init(int n,int m)
    {
        this->n=n;
        this->m=m;
        memset(left,-1,sizeof(left));
		memset(g,0,sizeof(g));
    }

    bool match(int u)
    {
        for(int v=1;v<=m;v++)
        {
            if(g[u][v] && !vis[v])
            {
                vis[v]=true;
                if(left[v]==-1 || match(left[v]))
                {
                    left[v]=u;
                    return true;
                }
            }
        }
        return false;
    }

    int solve()
    {
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            memset(vis,0,sizeof(vis));
            if(match(i)) ans++;
        }
        return ans;
    }
}MM;
int T;
int cas = 1;
struct Node
{
	int like;
	int unlike;
}p1[maxn],p2[maxn];

int main()
{
	int x1,x2,p;
	while (scanf("%d%d%d",&x1,&x2,&p)!=EOF)
	{
		int num1=0;
		int num2 = 0;
		for (int i = 1;i<=p;i++)
		{
			char c1,c2;
			int v1,v2;
			scanf(" %c %d %c %d",&c1,&v1,&c2,&v2);
			if (c1=='D')
			{
				p1[++num1].like=v1;
				p1[num1].unlike=v2;
			}
			else
			{
				p2[++num2].like=v1;
				p2[num2].unlike=v2;
			}
		}
		MM.init(num1,num2);
		for (int i = 1;i<=num1;i++)
			for (int j = 1;j<=num2;j++)
			{
				if (p1[i].like == p2[j].unlike || p1[i].unlike==p2[j].like)
				{
					MM.g[i][j]=1;
				}
			}
		printf("%d\n",p-MM.solve());
	}
    return 0;
}

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. 
         
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值