UVA 12168 Cat vs. Dog(最大独立集)

这个看的出来是求最大独立集,但是一开始一直是猫和狗矛盾,用毛和狗在做最大匹配。

但是题目要求是最多满足多少人的要求,才发现,是人的最大独立集。

#include<iostream>
#include<cstring>
using namespace std;
const int maxn=500+5;
bool line[maxn][maxn],used[maxn];
int match[maxn],c,d;
struct person{
    int like,unlike;
    person(int l=0,int ul=0): like(l),unlike(ul) {}
}cat[maxn],dog[maxn];
bool find(int i)
{
    for(int j=1;j<=d;j++)
    {
        if(line[i][j]==true&&used[j]==false)
        {
            used[j]=true;
            if(match[j]==-1||find(match[j]))
            {
                match[j]=i;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(line,false,sizeof(line));
        memset(match,-1,sizeof(match));
        int n,m,v;
        scanf("%d%d%d",&n,&m,&v);
        c=0,d=0;
        char s1[5],s2[5];
        for(int i=1;i<=v;i++)
        {
            scanf("%s%s",s1,s2);
            int a=0,b=0;
            for(int j=1;s1[j];j++) a=a*10+s1[j]-'0';
            for(int j=1;s2[j];j++) b=b*10+s2[j]-'0';
            if(s1[0]=='C') {cat[++c]=person(a,b);}
            else {dog[++d]=person(a,b);}
        }
        for(int i=1;i<=c;i++)
        {
            for(int j=1;j<=d;j++)
            {
                if(cat[i].unlike==dog[j].like||cat[i].like==dog[j].unlike) line[i][j]=true;
            }
        }
        int ans=0;
        for(int i=1;i<=c;i++){
            memset(used,false,sizeof(used));
            if(find(i)) ans+=1;
        }
        printf("%d\n",v-ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值