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

 #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. 前端面试问题html汇总

    1.对WEB标准以及W3C的理解与认识 结构层 html :表示层  css:行为层:js; 标签闭合.标签小写.不乱嵌套.提高搜索机器人搜索机率.使用外链css和js脚本.结构行为表现的分离.文件下 ...

  2. HDOJ4261 Estimation

    一道需要用堆初始化的\(DP\) 原题链接 显然对于每一个部分,当\(b[i]\)为\(a\)对于部分的中位数时,差错最小.设\(S(x,y)\)表示\(x\sim y\)这一部分的差错. \(DP\ ...

  3. (转帖)CentOS最常用命令及快捷键整理

    原文:http://www.centoscn.com/CentOS/help/2014/0306/2493.html 最近开始学Linux,在VMware Player中安装了CentOS 6.4.为 ...

  4. ui设计学习路线图分享送给初学者

    本文来源:千锋UI设计 Ui设计师就业市场前景及薪资很可观,而且现在市场上对于ui设计师的人才需求也很大,所以,现在越来越多的人看好这一行业,纷纷投入到ui设计的学习中来,那么想新手想要学好ui设计, ...

  5. JDK8集合类源码解析 - HashMap

    java为数据结构中的映射定义了一个接口java.util.Map,此接口主要有四个常用的实现类,分别是HashMap.Hashtable.LinkedHashMap和TreeMap HashMap ...

  6. Chrome firefox ie等浏览器空格(&nbsp;)兼容问题

    使用(&nbsp:)空格浏览器之间,显示的不一样,对不起等现象. 解决方案: 用半角空格&ensp:或者全角空格&emsp:就可以了,&ensp:相当于半格中文字符的宽 ...

  7. django网页图片验证码功能

    在一个正常的登录系统中,验证码是非常重要的,用于识别人机,毕竟我们都知道,这个世界中存在着万恶的爬虫,验证码有很多种方式,有图片的,有邮件的,有短信的,有拼图的,不管什么样的验证码,目的都是验证访问用 ...

  8. 使用 IntelliTrace 调试应用程序

    IntelliTrace 如何能够大幅改善您的日常开发活动,并提升您快速轻松诊断问题的能力,而不必重新启动应用程序和使用传统的“中断-单步执行-检查”技术进行调试.介绍了组织如何能够通过在测试过程中收 ...

  9. 【Web】Sublime Text 3 连接sftp/ftp(远程服务器)

    在 Win 下常用 Xftp 软件来和远程服务传递文件,但是要是在项目开发的时候频繁的将远程文件拖到本地编辑然后再传回远程服务器,那真是麻烦无比,但是Sublime中SFTP插件,它让这世界美好了许多 ...

  10. 449. Serialize and Deserialize BST

    https://leetcode.com/problems/serialize-and-deserialize-bst/#/description Serialization is the proce ...