SPOJ4206Fast Maximum Matching(hopcroft-karp)
题目大意:裸的二分匹配。
题目分析:数据比较强,用来测模版的。这题用hungry跑着会比较吃力,所以用hopcroft-karp算法。这个算法较hungry高效是因为每次bfs找到一个增广路集,然后用dfs进行多路增广,同时找多条增广路,从而效率大增。其实怎么看hk算法都是个没有边权的dinic啊。
参照着wikipedia 敲了一个hk,效率貌似不高啊。。。
详情请见代码:
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 50001;
const int M = 150001;
const int inf = 0x3f3f3f3f; int head[N];
struct node
{
int to,next;
}g[M];
int m,n,p,num;
int matchx[N],matchy[N],que[N],dis[N];
void build(int s,int e)
{
g[num].to = e;
g[num].next = head[s];
head[s] = num ++;
}
bool bfs()
{
int i,j;
int front,rear;
front = rear = 0;
for(i = 1;i <= n;i ++)
{
if(!matchx[i])
{
dis[i] = 0;
que[rear ++] = i;
}
else
dis[i] = inf;
}
dis[0] = inf;
while(front != rear)
{
int u = que[front ++];
if(front == N)
front = 0;
for(i = head[u];~i;i = g[i].next)
{
int v = g[i].to;
if(dis[matchy[v]] == inf)
{
dis[matchy[v]] = dis[u] + 1;
que[rear ++] = matchy[v];
if(rear == N)
rear = 0;
}
}
}
return dis[0] != inf;
}
bool dfs(int u)
{
int i,v;
for(i = head[u];~i;i = g[i].next)
{
v = g[i].to;
if(dis[matchy[v]] == dis[u] + 1)
if(matchy[v] == 0 || dfs(matchy[v]))
{
matchx[u] = v;
matchy[v] = u;
return true;
}
}
dis[u] = inf;
return false;
} void Hopcroft_Karp()
{
memset(matchx,0,sizeof(matchx));
memset(matchy,0,sizeof(matchy));
int ans = 0;
while(bfs())
{
for(int i = 1;i <= n;i ++)
if(!matchx[i])
if(dfs(i))
ans ++;
}
printf("%d\n",ans);
}
int main()
{
int a,b;
while(scanf("%d",&n) != EOF)
{
memset(head,-1,sizeof(head));
num = 1;
scanf("%d%d",&m,&p);
while(p --)
{
scanf("%d%d",&a,&b);
build(a,b);
}
Hopcroft_Karp();
}
return 0;
}
SPOJ4206Fast Maximum Matching(hopcroft-karp)的更多相关文章
- 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 ...
- [Codeforces Round #508 (Div. 2)][Codeforces 1038E. Maximum Matching]
前几天给舍友讲这题的时候感觉挺有意思的,就贴上来吧... 题目链接:1038E - Maximum Matching 题目大意:有\(n\)个棒子,每个条两端有颜色\(c1,c2\)以及他的价值\(v ...
- Codeforces 1038 E - Maximum Matching
E - Maximum Matching 思路: 欧拉图 定理:一个度数为奇数的点的个数小于等于2的联通图存在欧拉回路 对于这道题目的图,点的个数为4,所以最坏的情况下4个点的度数都为奇数,在这种情况 ...
- Codeforces Round #508 (Div. 2) E. Maximum Matching(欧拉路径)
E. Maximum Matching 题目链接:https://codeforces.com/contest/1038/problem/E 题意: 给出n个项链,每条项链左边和右边都有一种颜色(范 ...
- [codeforces 508E]Maximum Matching
题目:Maximum Matching 传送门:http://codeforces.com/contest/1038/problem/E 分析: 一个块拥有{color1,val,color2},两个 ...
- SPOJ 4206 Fast Maximum Matching (二分图最大匹配 Hopcroft-Carp 算法 模板)
题目大意: 有n1头公牛和n2头母牛,给出公母之间的m对配对关系,求最大匹配数.数据范围: 1 <= n1, n2 <= 50000, m <= 150000 算法讨论: 第一反应 ...
- CF1038E Maximum Matching 搜索/区间DP
题目传送门:http://codeforces.com/problemset/problem/1038/E 题意:给出$N$个方块,每个方块有左右两种颜色$a,b$(可以翻转使左右两种颜色交换)和一个 ...
- 最大匹配算法 (Maximum Matching)
之所以研究这个算法,是因为最近在研究NLP中文的分词,所谓分词就是将一个完整的句子,例如“计算语言学课程有意思”,分解成一些词组单元“计算语言学,课程,有,意思”. “最大匹配法” 在中文分词中有所应 ...
- CF#508 1038E Maximum Matching
---题面--- 题解: 感觉还是比较妙的,复杂度看上去很高(其实也很高),但是因为n只有100,所以还是可以过的. 考虑一个很暴力的状态f[i][j][x][y]表示考虑取区间i ~ j的方格,左右 ...
随机推荐
- httpcontext in asp.net unit test
[TestMethod] [HostType("ASP.NET")] [UrlToTest("http://localhost:25153/qq/a.aspx" ...
- 备忘·添加SublimeText3右键菜单
因为用的sublimeText3是免安装版,打开未关联文件,略有麻烦,所以搜了一下,发现几种解决办法,其中INF文件的方法很喜欢,备份下 [Version] Signature="$Wind ...
- [转载]C#读写配置文件(XML文件)
.xml文件格式如下 [xhtml] view plaincopy <?xml version="1.0" encoding="utf-8"?> & ...
- Codeforces Round #211 (Div. 2)
难得一次比赛能够自己成功A掉四个题: A题:水题,模拟一下就行: #include <iostream> #include <cstdio> using namespace s ...
- 【原创】MIPS中断系统的板级验证及实例测试
“五一”假期前后这约五天时间,终于将MIPS中断系统进行了板级验证及实例测试.因为老师给的交叉编译工具不会用,所以测试代码完全用MIPS汇编编写.使用MARS而没有用QtSpim,其实我觉得SPIM这 ...
- hdu4597Play Game(记忆化)
链接 通化邀请赛的题 貌似不怎么难 记忆化DP 状态方程类似于2维的 只是变成了4维 每次有四种方向去搜 取最棒的 判断好边界条件 #include <iostream> #includ ...
- BZOJ_1610_[Usaco2008_Feb]_Line连线游戏_(计算几何基础+暴力)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1610 给出n个点,问两两确定的直线中,斜率不同的共有多少条. 分析 暴力枚举直线,算出来斜率放 ...
- 【转】NI语法 JNI参考 JNI函数大全
原文网址:http://blog.sina.com.cn/s/blog_5de73d0b0101chk1.html 一.对照表 Java类型 本地类型 描述boolean ...
- Android 动画-alpha(渐变透明度动画效果)
今天苦于思索应用如何美观,首先从载入页面的第一眼开始,Android动画分为四种:alpha(渐变透明度),scale(渐变尺寸伸缩),translate(画面转换位置移动),rotate(画面转移旋 ...
- [Stephen]关于Ext.net fileupload 的兼容性解决问题
在firefox下,利用fileupload上传图片后,通过后端将image 的src路径进行更新,刷新前段界面显示没有问题. 但是在以IE为内核的360中,这种上传后的更新导致一个命名为Action ...