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

87 篇文章 0 订阅

Cat VS Dog

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)
Total Submission(s): 3858    Accepted Submission(s): 1387


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 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.
 

Source
 

Recommend
xubiao

题目大意:

    有一些猫和狗,和一些人,每个人有一个喜欢的动物和不喜欢的动物(它们一定是一只猫和一只狗或一只狗和一只猫)。现在要移走一些动物,如果一个人喜欢的动物没有被移走,但不喜欢的被移走了,那么他就很高兴。问最多能让多少人高兴。


解题思路:

    题面非常强烈的诱导我们把猫狗作为结点,把人作为边,但是这样就成了一个点带权的最大独立集,无法求解。这时我们就要考虑把点和边交换。把人作为点,如果两个人的爱好有冲突就在他们之间连边,这样建图最大独立集就是答案。


AC代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <string>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define fi first
#define se second
#define mem(a,b) memset((a),(b),sizeof(a))

const int MAXV=500+3;
pair<string,string> like[MAXV];//每个人喜欢和不喜欢的动物
int N,M,P;
vector<int> G[MAXV];
int match[MAXV];
bool used[MAXV];

void init()
{
    for(int i=0;i<P;++i)
        G[i].clear();
}

bool dfs(int v)
{
    used[v]=true;
    for(int i=0;i<G[v].size();++i)
    {
        int u=G[v][i],w=match[u];
        if(w<0||!used[w]&&dfs(w))
        {
            match[v]=u;
            match[u]=v;
            return true;
        }
    }
    return false;
}

int bipartite_matching()//匈牙利算法求最大匹配
{
    int res=0;
    mem(match,-1);
    for(int v=0;v<P;++v)
    {
        if(match[v]<0)
        {
            mem(used,0);
            if(dfs(v))
                ++res;
        }
    }
    return res;
}

int main()
{
    cin.sync_with_stdio(false);
    while(cin>>N>>M>>P)
    {
        init();
        for(int i=0;i<P;++i)
            cin>>like[i].fi>>like[i].se;
        for(int i=0;i<P;++i)
            for(int j=i+1;j<P;++j)
                if(like[i].fi==like[j].se||like[i].se==like[j].fi)
                {
                    G[i].push_back(j);
                    G[j].push_back(i);
                }
        cout<<P-bipartite_matching()<<'\n';
    }
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值