先把代码贴上,其他南京回来再补了。。

 #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <ctime>
#include <vector>
#include <list>
#include <queue>
#define M0(a) memset(a, 0, sizeof(a))
#define Inf 0x7fffffff
#define MXN 2110
using namespace std;
vector<int> edge[MXN];
int dx[MXN], dy[MXN];
int a[][];
bool state[];
int match[], Mx[];
int n, m; void add(int x, int y){
edge[x].push_back(y);
edge[y].push_back(x);
} void init(){
M0(a);
int x, y;
for (int i = ; i <= n + m; ++i)
edge[i].clear();
for (int i = ; i <= n; ++i){
scanf("%d%d", &x, &y);
a[x][y] = i;
a[x + ][y] = i;
}
for (int i = ; i <= m; ++i){
scanf("%d%d", &x, &y);
int now = a[x][y];
if (now) add(i + n, now);
now = a[x][y + ];
if (now) add(i + n, now);
}
} bool bfs(){
queue<int> q;
bool flag = false;
M0(dx);
M0(dy);
for (int i = ; i <= n; ++i)
if (!Mx[i]){
q.push(i);
dx[i] = ;
}
int u, v;
while (!q.empty()){
u = q.front();
for (int i = ; i < edge[u].size(); ++i){
v = edge[u][i];
if (dy[v]) continue;
dy[v] = dx[u] + ;
if (match[v] == -) flag = true;
else {
q.push(match[v]);
dx[match[v]] = dy[v] + ;
}
}
q.pop();
}
return flag;
} bool search_path(int u){
int v;
for (int i = ; i < edge[u].size(); ++i){
v = edge[u][i];
if (dy[v] != dx[u] + ) continue;
if (!state[v]){
state[v] = true;
if (match[v] == - || search_path(match[v])){
Mx[u] = v;
match[v] = u;
return true;
}
}
}
return false;
} void HK(){
M0(Mx);
int M = ;
memset(match, -, sizeof(match));
while (bfs()){
M0(state);
for (int i = ; i <= n; ++i)
if(!Mx[i] && search_path(i)) ++M;
}
printf("%d\n", n + m - M);
} int main(){
// freopen("hdu4619.in","r", stdin);
// freopen("hdu4619.out","w", stdout);
while (scanf("%d%d", &n, &m) != EOF){
if (!n) break;
init();
HK();
}
// fclose(stdin); fclose(stdout);
}

二分图匹配-HK算法的更多相关文章

  1. hdu2389 Rain on your Parade 二分图匹配--HK算法

    You’re giving a party in the garden of your villa by the sea. The party is a huge success, and every ...

  2. HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))

    Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  3. USACO 4.2 The Perfect Stall(二分图匹配匈牙利算法)

    The Perfect StallHal Burch Farmer John completed his new barn just last week, complete with all the ...

  4. 训练指南 UVALive - 4043(二分图匹配 + KM算法)

    layout: post title: 训练指南 UVALive - 4043(二分图匹配 + KM算法) author: "luowentaoaa" catalog: true ...

  5. HDU 5727 - Necklace - [全排列+二分图匹配][Hopcroft-Karp算法模板]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5727 Problem DescriptionSJX has 2*N magic gems. ...

  6. Codevs 1222 信与信封问题 二分图匹配,匈牙利算法

    题目: http://codevs.cn/problem/1222/ 1222 信与信封问题   时间限制: 1 s   空间限制: 128000 KB   题目等级 : 钻石 Diamond 题解 ...

  7. HDU1507 Uncle Tom's Inherited Land* 二分图匹配 匈牙利算法 黑白染色

    原文链接http://www.cnblogs.com/zhouzhendong/p/8254062.html 题目传送门 - HDU1507 题意概括 有一个n*m的棋盘,有些点是废的. 现在让你用1 ...

  8. (转)二分图匹配匈牙利算法与KM算法

    匈牙利算法转自于: https://blog.csdn.net/dark_scope/article/details/8880547 匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名 ...

  9. BZOJ1059 [ZJOI2007]矩阵游戏 二分图匹配 匈牙利算法

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1059 题意概括 有一个n*n(n<=200)的01矩阵,问你是否可以通过交换整行和整列使得左 ...

随机推荐

  1. CH#56C 异象石

    一道LCA 原题链接 先跑一边\(dfs\),求出每个节点的时间戳,如果我们将有异象石的节点按时间戳从小到大的顺序排列,累加相邻两节点之间的距离(首尾相邻),会发现总和就是答案的两倍. 于是我们只需要 ...

  2. Spring 注解原理(三)@Qualifier @Value

    Spring 注解原理(三)@Qualifier @Value Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 一.Aut ...

  3. XXE总结

    0x00 目录 0x01 XML基础 定义:一种标记电子文件使其具有结构性的标记语言,可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. XML文档结构包括XML声明.D ...

  4. MySQL复制(Replication)

    引自:http://www.cnblogs.com/hustcat/archive/2009/12/19/1627525.html 1.复制概述 1.1.复制解决的问题数据复制技术有以下一些特点:(1 ...

  5. 图解http学习笔记【一】

    不想单纯的把书里的知识点罗列一遍 这周,我们的安全代码终于改完了.我在微信上报了个叫 一修读书的课程,现在已经听了6天.感觉并不是很神奇,聊胜于无.倒是趁着当当搞活动买回来好几本书,其中就有这本图解h ...

  6. Spring Boot学习笔记:kafka应用

    Kafka作为众多Java消息中间件之一,有诸多优点.本文讲解Kafka的应用.学习一个新的知识点,建议先找一个demo,越简单越好的demo,跑通这个demo,了解大致原理,然后在分析细节,详细了解 ...

  7. 2017/2/11CSS基础

    一:html中div: 1.DIV标签应用于 Style Sheet(样式表)方面会更显威力,它最终目的是给设计者另一种组织能力,有 Class.Style.title.ID 等属性. 2.<d ...

  8. 进度条的制作unity

    不说了直接上代码: LoadingPanel: using UnityEngine;using System.Collections;using UnityEngine.UI;using UnityE ...

  9. raiDrive添加坚果云

    把坚果云网盘映射到本地. 使用raidrive 中webDAV添加坚果云. 1.登录自己的坚果云网页版,点“安全选项”,右侧有第三方应用管理,生成密码,按照示例填入第三方应用即可. 2.注意:暂时此处 ...

  10. vue 开发系列(七) 路由配置

    概要 用 Vue.js + vue-router 创建单页应用,是非常简单的.使用 Vue.js ,我们已经可以通过组合组件来组成应用程序,当你要把 vue-router 添加进来,我们需要做的是,将 ...