#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
const int maxn = 3010;
const int INF = 1<<28;
int dx[maxn], dy[maxn];
int cx[maxn], cy[maxn];
vector <int> G[maxn];
int dis;
int n, m;
bool vis[maxn];
bool search()
{
queue <int> Q;
int dis = INF;
memset(dx, -1, sizeof(dx));
memset(dy, -1, sizeof(dy));
for(int i = 0; i < n; i++)
if(cx[i] == -1)
{
Q.push(i);
dx[i] = 0;
}
while(!Q.empty())
{
int u = Q.front(); Q.pop();
if(dx[u] > dis)
break;
for(int i = 0; i < G[u].size(); i++)
{
int v = G[u][i];
if(dy[v] == -1)
{
dy[v] = dx[u] + 1;
if(cy[v] == -1)
dis = dy[v];
else
{
dx[cy[v]] = dy[v] + 1;
Q.push(cy[v]);
}
}
}
}
return dis != INF;
} bool dfs(int u)
{
for(int i = 0; i < G[u].size(); i++)
{
int v = G[u][i];
if(vis[v])
continue;
vis[v] = true;
if(dy[v] == dx[u]+1)
{
if(cy[v] != -1 && dy[v] == dis)
continue;
if(cy[v] == -1 || dfs(cy[v]))
{
cy[v] = u;
cx[u] = v;
return true;
}
}
}
return false;
}
int match()
{
int ans = 0;
memset(cx, -1, sizeof(cx));
memset(cy, -1, sizeof(cy));
while(search())
{
memset(vis, 0, sizeof(vis));
for(int i = 0; i < n; i++)
if(cx[i] == -1 && dfs(i))
ans++;
}
return ans;
}

Hopcroft-Karp算法模版的更多相关文章

  1. hdu2389二分图之Hopcroft Karp算法

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

  2. 网络流之最大流Dinic算法模版

    /* 网络流之最大流Dinic算法模版 */ #include <cstring> #include <cstdio> #include <queue> using ...

  3. [算法模版]Tarjan爷爷的几种图论算法

    [算法模版]Tarjan爷爷的几种图论算法 前言 Tarjan爷爷发明了很多图论算法,这些图论算法有很多相似之处(其中一个就是我都不会).这里会对这三种算法进行简单介绍. 定义 强连通(strongl ...

  4. [算法模版]Prim-完全图最小生成树

    [算法模版]Prim-完全图最小生成树 众所周知,对于常用的Kruskal算法,算法复杂度为\(O(m \log m)\).这在大多数场景下已经够用了.但是如果遇到及其稠密的完全图,Prim算法就能更 ...

  5. [算法模版]子序列DP

    [算法模版]子序列DP 如何求本质不同子序列个数? 朴素DP 复杂度为\(O(nq)\).其中\(q\)为字符集大小. \(dp[i]\)代表以第\(i\)个数结尾的本质不同子序列个数.注意,这里对于 ...

  6. [算法模版]AC自动机

    [算法模版]AC自动机 基础内容 板子不再赘述,OI-WIKI有详细讲解. \(query\)函数则是遍历文本串的所有位置,在文本串的每个位置都沿着\(fail\)跳到根,将沿途所有元素答案++.意义 ...

  7. [算法模版]Link-Cut-Tree

    [算法模版]Link-Cut-Tree 博主懒本博客只对现有博客进行补充,先直接放隔壁链接. FlashHu-LCT总结 Menci-LCT学习笔记 make-root操作 make-root操作用于 ...

  8. 最大流算法之Ford-Fulkerson算法与Edmonds–Karp算法

    引子 曾经很多次看过最大流的模板,基础概念什么的也看了很多遍.也曾经用过强者同学的板子,然而却一直不会网络流.虽然曾经尝试过写,然而即使最简单的一种算法也没有写成功过,然后对着强者大神的代码一点一点的 ...

  9. 一个人的旅行(hdu2066)Dijkstra算法模版

    一个人的旅行 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  10. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) 【莫队算法模版】

    任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=2038 题意概括: 有 N 只袜子(分别编号为1~N),有 M 次查询 (L, R)里面随机 ...

随机推荐

  1. C++14介绍

    C++14标准是 ISO/IEC 14882:2014 Information technology -- Programming languages -- C++ 的简称[1]  .在标准正式通过之 ...

  2. 在线获取访客QQ号码的原理及实现方法

    原文地址:http://www.piaoyi.org/network/get-qq-haoma-js.html 正 文: 最近,飘易收到不少在线获取网站访客QQ号码的促销推广邮件,有不少商用网站挖掘了 ...

  3. 9款超酷的jQuery/CSS3插件

    Article From here: http://js.itivy.com/?p=1883 1.jQuery向前滑动切换焦点图 这款jQuery焦点图非常绚丽,切换图片的时候每张图片是向前滑动的,很 ...

  4. AJAX提交到Handler.ashx一般处理程序返回json数据 (字符串拼接方式)

    <%@ WebHandler Language="C#" Class="Handler" %> using System; using System ...

  5. cf公式专场-续

    Benches Time Limit:500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  6. mysql 初始化

    一.centos7下mysql 安装配置 yum -y install mariadb* systemctl start mariadb.service systemctl enable mariad ...

  7. LeetCode Day1

    Palindrome Linked List /** * LeetCode: Palindrome Linked List * Given a singly linked list, determin ...

  8. HTTP请求的基本概念 HTTP请求头和响应头的含义

    1,HTTP请求的基本概念    TCP/UPD/HTTP    *2,HTTP请求头和响应头的含义  请求头:  Accept: text/html,image/*(浏览器可以接收的类型)  Acc ...

  9. android系统将普通应用升级为系统应用

    作为一名程序员,有的时候并不是使用软件,而是去改造软件,不仅仅只是会编程而已,还要满足客户的需求.这样,才能开发出符合客户需求的应用,在关于到涉及到android底层的应用的时候,手机就需要root了 ...

  10. oracle11g用户名密码不区分大小写

    oracle 11g 以前的版本的用户名和密码是不区分大小写的; oracle 11g 用户名和密码默认区分大小写,可更改alter system set sec_case_sensitive_log ...