UVALive 3415 浅谈二分图最大点独立集架构建模

这里写图片描述
世界真的很大
(PS:P站没了找图越来越艰难了233,但是总有办法)
看来非得是要把二分图的所有性质过一遍手才觉得心安
然后是这个二分图最大点独立集

看题先:

description:

有一个思想有点保守的老师,班级有n个人,他可以带一些人出去玩,其中他要防止有些人萌生爱意,他决定带出去的学生至少要满足下面4个条件之一:(1)身高差 > 40(2)性别相同(3)喜欢的音乐不同(4)喜欢的体育比赛相同。也就是说这些带出去的人任意两个人都至少满足4个条件之一。问你他能带出去的最大的人数。

input:

The rst line of the input consists of an integer T 100 giving the number of test cases. The rst line
of each test case consists of an integer N 500 giving the number of pupils. Next there will be one
line for each pupil consisting of four space-separated data items:
an integer h giving the height in cm;
a character F' for female orM’ for male;
a string describing the preferred music style;
a string with the name of the favourite sport.
No string in the input will contain more than 100 characters, nor will any string contain any
whitespace

output:

For each test case in the input there should be one line with an integer giving the maximum number
of eligible pupils

这个老师想要选出来最多的人数,这里面两两之间都至少满足以上关系中的一条。
换句话说,如果两个人得关系以上4条全都不满足,这两个人就是“你去我不去”的状况
(Ps:可悲啊)

有很多对这样的状况,如果把这样的条件抽象成一条边,那么就可以建一个图,这张图的每一个边都只能选一个点,问最多能选多少个点,这样的点的集合被称作“独立集”,我们要求的就是“最大独立集”
说到最大独立集,一般能想到的就是二分图的最大独立集了,否则我们也求不来
那么对于这个情况,我们只能去确认这张图的却是一张二分图。
发现只有当四个条件必须全部不满足才连边
四个条件中有一个是“异性”。。。。
非男即女
男男之间,女女之间不可能连边。按男女分的话,就是二分图了
换句话说题目里出现性别条件,就可以考虑二分图了

然后连边跑最大独立集就好
定理:最大独立集 == 点数 - 最大匹配

完整代码:

#include<stdio.h>
#include<string.h>
#include<cstring>
#include<algorithm>
using namespace std;

struct edge
{
    int v,last;
}ed[2000010];

int n,T,ans,num,tot;
int head[1010],match[1010],book[1010],h[1010];
char gdr[1010][110],msc[1010][110],spt[1010][110];

void add(int u,int v)
{
    num++;
    ed[num].v=v;
    ed[num].last=head[u];
    head[u]=num;
}

bool check(int x,int y)
{
    if(abs(h[x]-h[y])>40) return false ;
    if(!strcmp(gdr[x],gdr[y])) return false ; 
    if(strcmp(msc[x],msc[y])) return false ;
    if(!strcmp(spt[x],spt[y])) return false ;
    return true ;
}

int dfs(int u)
{
    for(int i=head[u];i;i=ed[i].last)
    {
        int v=ed[i].v;
        if(book[v]!=tot)
        {
            book[v]=tot;
            if(!match[v] || dfs(match[v]))
            {
                match[u]=v,match[v]=u;
                return 1;
            }
        }
    }
    return 0;
}

void init()
{
    memset(head,0,sizeof(head));
    memset(match,0,sizeof(match));
    memset(h,0,sizeof(h));
    num=tot=ans=0;
}

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&h[i]);
            scanf("%s",gdr[i]);
            scanf("%s",msc[i]);
            scanf("%s",spt[i]);
        }
        for(int i=1;i<=n;i++)
            for(int j=1;j<i;j++)
                if(check(i,j)) add(j,i),add(i,j);
        for(int i=1;i<=n;i++)
        {
            tot++;
            if(!match[i]) ans+=dfs(i);
        }
        printf("%d\n",n-ans);
    }
    return 0;
}
/*
EL PSY CONGROO
*/

嗯,就是这样

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值