HDU 2444 The Accomodation of Students

匈牙利算法 

https://blog.csdn.net/sixdaycoder/article/details/47680831

将找增广路径转化为找到出发点集合m里的另一点,match其实只对二分图非起点集的另一半有效

但是这道题并不明确哪一部分是起点集,我们是全部都搜,所以会重复,最后结果除以二

还有就是染色法,一开始是边输入边赋0,1。。。

个人感觉染色法两个关键,1,一定要是连通图,比如1,2认识,3,4认识,2,3再一认识,全乱套

2 取模运算

#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <cstring>
#include <stdio.h>
#include <queue>
#include <algorithm>
#include <vector>

#define MAX 210

using namespace std;

int n,m,a,b,flag,ans;
int g[MAX][MAX];
int mark[MAX];
int color[MAX];
int match[MAX];
queue<int >q;

int findPath(int x){
    int y;
    for(int i=1;i<=n;i++){
        if(g[x][i]){
            if(!mark[i]){
                mark[i]=1;
                if(match[i]==-1||findPath(match[i])){
                    match[i]=x;
                    return 1;
                }
            }
        }
    }
    return 0;
}

int BFS(){
    while(!q.empty())
        q.pop();
    memset(mark,0,sizeof(mark));
    memset(color,-1,sizeof(color));

    color[1]=0;
    q.push(1);
    while(!q.empty()){
        int x=q.front();
        q.pop();
        mark[x]=1;

        for(int i=1;i<=n;i++){
            if(g[x][i]){
                if(color[i]==-1){
                    color[i]=(color[x]+1)%2;
                    q.push(i);
                }
                else if(color[x]+color[i]!=1)
                    return false;
            }
        }
    }
    return true;
}

int main()
{
    while(~scanf("%d%d",&n,&m)){
        ans=0;
        memset(g,0,sizeof(g));
        memset(match,-1,sizeof(match));
        memset(color,-1,sizeof(color));

        while(m--){
            scanf("%d%d",&a,&b);
            g[a][b]=g[b][a]=1;
        }
        if(!BFS()){
            printf("No\n");
        }
        else {
            for(int i=1;i<=n;i++){    //之前是只二分图半块做起点,现在相当全图都搜,match数量翻倍
                memset(mark,0,sizeof(mark));
                if(findPath(i))
                    ans++;
            }
            printf("%d\n",ans/2);
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值