Hopcroft-Carp算法模板【二分图匹配】
来源:互联网 发布:seo三人行 编辑:程序博客网 时间:2024/05/16 13:47
模板://hdu 2063
Hopcroft-Carp 时间复杂度为 O(sqrt(V)*E);
而匈牙利算法为 O(V*E);
#include <stdio.h>#include <ctime>#include <math.h>#include <limits.h>#include <complex>#include <string>#include <functional>#include <iterator>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <set>#include <map>#include <list>#include <bitset>#include <sstream>#include <iomanip>#include <fstream>#include <iostream>#include <ctime>#include <cmath>#include <cstring>#include <cstdio>#include <time.h>#include <ctype.h>#include <string.h>#include <assert.h>using namespace std;const int N = 1005;const int INF = 1 << 28;int g[N][N];int Mx[N];int My[N];int dx[N];int dy[N];bool used[N];int Nx, Ny, dis;bool searchP(){ dis = INF; int i, v, u; std::queue<int> Q; memset(dx, -1, sizeof(dx)); memset(dy, -1, sizeof(dy)); for (i = 0; i<Nx; i++) { if (Mx[i] == -1) { Q.push(i); dx[i] = 0; } } while (!Q.empty()) { u = Q.front(); Q.pop(); if (dx[u]>dis) break; for (v = 0; v<Ny; v++) { if (g[u][v] && dy[v] == -1) { dy[v] = dx[u] + 1; if (My[v] == -1) dis = dy[v]; else { dx[My[v]] = dy[v] + 1; Q.push(My[v]); } } } } return dis != INF;}bool DFS(int u){ int v; for (v = 0; v<Ny; v++) { if (g[u][v] && !used[v] && dy[v] == dx[u] + 1) { used[v] = true; if (My[v] != -1 && dy[v] == dis) continue; if (My[v] == -1 || DFS(My[v])) { My[v] = u; Mx[u] = v; return true; } } } return false;}int Hungary(){ int u; int ret = 0; memset(Mx, -1, sizeof(Mx)); memset(My, -1, sizeof(My)); while (searchP()) { memset(used, false, sizeof(used)); for (u = 0; u<Nx; u++) if (Mx[u] == -1 && DFS(u)) ret++; } return ret;}int main(){ int k, u, v; while (~scanf("%d", &k) ,k) { scanf("%d%d", &Nx, &Ny); memset(g, 0, sizeof(g)); Ny = Nx>Ny ? Nx : Ny; while (k--) { scanf("%d%d", &u, &v); u--; v--; g[u][v] = 1; } int ans = Hungary(); printf("%d\n", ans); } return 0;}
0 0
- Hopcroft-Carp算法模板【二分图匹配】
- 二分图匹配——Hopcroft-Carp算法模板
- 二分图匹配Hopcroft-Carp算法介绍
- HDOJ 2389 - Hopcroft-Carp模板二分图最大匹配
- HDU 2389 Rain on your Parade (二分图匹配(Hopcroft-Carp的算法模板))
- 二分图匹配(Hopcroft-Carp的算法) 模版
- 二分匹配的Hopcroft-Carp算法
- 二分匹配的Hopcroft-Carp算法
- 二分匹配的Hopcroft-Carp算法
- hdoj 2389 二分图 Hopcroft-Carp 模板
- HDU 2389 Rain on your Parade(二分匹配+Hopcroft-Carp算法模板题)
- 【二分图匹配】Hopcroft-Karp算法模板
- hdu2389 Rain on your Parade(二分匹配hopcroft-carp算法)
- HDU2389_Rain on your Parade_二分图匹配::Hopcroft-Carp模板题
- SPOJ Fast Maximum Matching(二分图最大匹配Hopcroft-Carp)
- 二分图最大匹配算法-Hopcroft-Karp模板
- HDU2389 Rain on your Parade 二分匹配 Hopcroft-Carp的算法+模版
- hdu 2389 Rain on your Parade(二分匹配Hopcroft-Carp算法模版)
- unity3D 音频播放
- css兼容问题
- poj 3259 Wormholes (BELLman—FOrd算法)(邻接矩阵表示)
- leetcode--ConvertSortedArraytoBinarySearchTree
- 天气预报---网络加载最新天气信息,截取JSON文件
- Hopcroft-Carp算法模板【二分图匹配】
- shiro权限项目中的简单应用
- 学习RecyclerView的一些东西
- Untiy Shader - 纹理贴图滚动
- 从输入网址到显示网页的全过程分析
- python网络编程学习笔记(9):数据库客户端
- leetcode--ConstructBinaryTreefromInorderandPostorderTraversal
- 自己实现内存拷贝函数memcpy
- 显示调用基类构造函数,迟绑定,VC++编译链接过程