hdu2444 The Accomodation of Students

PS: 二分图+最大匹配。 先判断是否为二分图同时对图进行染色,如果不是二分图输出No如果是二分图则将图分为左右两边的"标准二分图“,然后求解最大匹配

二分图没做几个题目, 就是凭着自己想着写的,代码不是很简洁。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
const int MAX = 210;

vector<int> G[MAX];
int n, m;
int color[MAX]; // 1 or -1

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

bool bipartite() {
    for(int i = 1; i <= n; i++) {
        if(color[i] == 0) {
            if(!dfs(i, 1)) {
                return false;
            }
        }
    }
    return true;
}

vector<int> g[MAX];
int from[MAX], tot;
int use[MAX];

int Match(int x) {
    for(int i = 0; i < (int)g[x].size(); i++) {
        int u = g[x][i];
        if(!use[u]) {
            use[u] = true;
            if(from[u]==-1 || Match(from[u])) {
                from[u] = x;
                return true;
            }
        }
    }
    return false;
}

int hungary() {
    tot = 0;
    memset(from, 255, sizeof(from));
    for(int i = 1; i <= n; i++) {
        if(color[i]==1) {
            memset(use, 0, sizeof(use));
            if(Match(i)) {
                ++tot;
            }
        }
    }
    return tot;
}

void build_map() {
    for(int i = 1; i <= n; i++) g[i].clear();
    for(int i = 1; i <= n; i++) {
        if(color[i]==1) {
            for(int j = 0; j < (int)G[i].size(); j++) {
                int &u = G[i][j];
                g[i].push_back(u);
            }
        }
    }
}

int main()
{
    while(scanf("%d%d", &n, &m) != EOF) {
        memset(color, 0, sizeof(color));
        int x, y;
        for(int i = 1; i <= 205; i++) G[i].clear();
        for(int i = 1; i <= m; i++) {
            scanf("%d%d", &x, &y);
            G[x].push_back(y);
            G[y].push_back(x);
        }
        bool ok = bipartite();
        if(!ok) {
            printf("No\n");
            continue;
        } else {
            build_map();
            int res = hungary();
            printf("%d\n", res);
        }
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值