Cat VS Dog(二分匹配)

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

题解:将矛盾的小朋友连线作图,使得最后的小朋友happy,就要去掉与他矛盾的小朋友,及去掉图中的所有连线,最终就把此题转换成了最小点覆盖问题,又最小点覆盖(不懂的看点进去看别人博客) = 最大匹配,这我就用匈利亚算法求解了,因为此题数据较小,我就用简单的邻接矩阵了。

//#include<bits/stdc++.h>
#include <unordered_map>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<set>
#include<climits>
#include<queue>
#include<cmath>
#include<stack>
#include<map>
using namespace std;
#define ll long long
#define MT(a,b) memset(a,b,sizeof(a));
const int INF  =  0x3f3f3f3f;
const int ONF  = -0x3f3f3f3f;
const int mod  =  1e9+7;
const int maxn =  5e3+5;
const int N    =  5e2+5;
const double PI  =  3.141592653589;
const double E   =  2.718281828459;

int n , m , k;
vector<int>like[2][N];
vector<int>hate[2][N];
int way[N][N],vis[N];
int Lx[N],Ly[N];

int dfs(int u)
{
    for(int v=1;v<=k;v++) if(way[u][v]&&!vis[v])
    {
        vis[v] = 1;
        if(Ly[v]==-1||dfs(Ly[v]))
        {
            Ly[v] = u; Lx[u] = v;
            return 1;
        }
    }
    return 0;
}

int match()
{
    int ans = 0;
    for(int i=1;i<=k;i++) if(Lx[i]==-1)
    {
        MT(vis , 0);
        ans += dfs(i);
    }
    return ans ;
}

void init()
{
    for(int l=0;l<2;l++) for(int i=0;i<=k;i++)
    {
        like[l][i].clear();
        hate[l][i].clear();
    }
    MT(vis,0);MT(way,0);
    MT(Ly,-1);MT(Lx,-1);
}

int main()
{
    while(cin>>n>>m>>k)
    {
        init();
        
        for(int i=1;i<=k;i++)
        {
            char x,y;int a,b;scanf(" %c%d %c%d",&x,&a,&y,&b);
            like[x-'C'][a].push_back(i);
            hate[y-'C'][b].push_back(i);
        }
        
        int num[2] = {n,m};
        for(int l=0;l<2;l++)
        for(int i=1;i<=num[l];i++)
        for(int j=0;j<like[l][i].size();j++)
        for(int k=0;k<hate[l][i].size();k++)
        {
            int u = like[l][i][j];
            int v = hate[l][i][k];
            way[u][v] = way[v][u] = 1;
        }

        cout<<k - match()/2<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值