题目请戳这里

题目大意:裸的二分匹配。

题目分析:数据比较强,用来测模版的。这题用hungry跑着会比较吃力,所以用hopcroft-karp算法。这个算法较hungry高效是因为每次bfs找到一个增广路集,然后用dfs进行多路增广,同时找多条增广路,从而效率大增。其实怎么看hk算法都是个没有边权的dinic啊。

参照着wikipedia 敲了一个hk,效率貌似不高啊。。。

详情请见代码:

  1. #include <iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. using namespace std;
  5. const int N = 50001;
  6. const int M = 150001;
  7. const int inf = 0x3f3f3f3f;
  8.  
  9. int head[N];
  10. struct node
  11. {
  12. int to,next;
  13. }g[M];
  14. int m,n,p,num;
  15. int matchx[N],matchy[N],que[N],dis[N];
  16. void build(int s,int e)
  17. {
  18. g[num].to = e;
  19. g[num].next = head[s];
  20. head[s] = num ++;
  21. }
  22. bool bfs()
  23. {
  24. int i,j;
  25. int front,rear;
  26. front = rear = 0;
  27. for(i = 1;i <= n;i ++)
  28. {
  29. if(!matchx[i])
  30. {
  31. dis[i] = 0;
  32. que[rear ++] = i;
  33. }
  34. else
  35. dis[i] = inf;
  36. }
  37. dis[0] = inf;
  38. while(front != rear)
  39. {
  40. int u = que[front ++];
  41. if(front == N)
  42. front = 0;
  43. for(i = head[u];~i;i = g[i].next)
  44. {
  45. int v = g[i].to;
  46. if(dis[matchy[v]] == inf)
  47. {
  48. dis[matchy[v]] = dis[u] + 1;
  49. que[rear ++] = matchy[v];
  50. if(rear == N)
  51. rear = 0;
  52. }
  53. }
  54. }
  55. return dis[0] != inf;
  56. }
  57. bool dfs(int u)
  58. {
  59. int i,v;
  60. for(i = head[u];~i;i = g[i].next)
  61. {
  62. v = g[i].to;
  63. if(dis[matchy[v]] == dis[u] + 1)
  64. if(matchy[v] == 0 || dfs(matchy[v]))
  65. {
  66. matchx[u] = v;
  67. matchy[v] = u;
  68. return true;
  69. }
  70. }
  71. dis[u] = inf;
  72. return false;
  73. }
  74.  
  75. void Hopcroft_Karp()
  76. {
  77. memset(matchx,0,sizeof(matchx));
  78. memset(matchy,0,sizeof(matchy));
  79. int ans = 0;
  80. while(bfs())
  81. {
  82. for(int i = 1;i <= n;i ++)
  83. if(!matchx[i])
  84. if(dfs(i))
  85. ans ++;
  86. }
  87. printf("%d\n",ans);
  88. }
  89. int main()
  90. {
  91. int a,b;
  92. while(scanf("%d",&n) != EOF)
  93. {
  94. memset(head,-1,sizeof(head));
  95. num = 1;
  96. scanf("%d%d",&m,&p);
  97. while(p --)
  98. {
  99. scanf("%d%d",&a,&b);
  100. build(a,b);
  101. }
  102. Hopcroft_Karp();
  103. }
  104. return 0;
  105. }

SPOJ4206Fast Maximum Matching(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. [Codeforces Round #508 (Div. 2)][Codeforces 1038E. Maximum Matching]

    前几天给舍友讲这题的时候感觉挺有意思的,就贴上来吧... 题目链接:1038E - Maximum Matching 题目大意:有\(n\)个棒子,每个条两端有颜色\(c1,c2\)以及他的价值\(v ...

  3. Codeforces 1038 E - Maximum Matching

    E - Maximum Matching 思路: 欧拉图 定理:一个度数为奇数的点的个数小于等于2的联通图存在欧拉回路 对于这道题目的图,点的个数为4,所以最坏的情况下4个点的度数都为奇数,在这种情况 ...

  4. Codeforces Round #508 (Div. 2) E. Maximum Matching(欧拉路径)

     E. Maximum Matching 题目链接:https://codeforces.com/contest/1038/problem/E 题意: 给出n个项链,每条项链左边和右边都有一种颜色(范 ...

  5. [codeforces 508E]Maximum Matching

    题目:Maximum Matching 传送门:http://codeforces.com/contest/1038/problem/E 分析: 一个块拥有{color1,val,color2},两个 ...

  6. SPOJ 4206 Fast Maximum Matching (二分图最大匹配 Hopcroft-Carp 算法 模板)

    题目大意: 有n1头公牛和n2头母牛,给出公母之间的m对配对关系,求最大匹配数.数据范围:  1 <= n1, n2 <= 50000, m <= 150000 算法讨论: 第一反应 ...

  7. CF1038E Maximum Matching 搜索/区间DP

    题目传送门:http://codeforces.com/problemset/problem/1038/E 题意:给出$N$个方块,每个方块有左右两种颜色$a,b$(可以翻转使左右两种颜色交换)和一个 ...

  8. 最大匹配算法 (Maximum Matching)

    之所以研究这个算法,是因为最近在研究NLP中文的分词,所谓分词就是将一个完整的句子,例如“计算语言学课程有意思”,分解成一些词组单元“计算语言学,课程,有,意思”. “最大匹配法” 在中文分词中有所应 ...

  9. CF#508 1038E Maximum Matching

    ---题面--- 题解: 感觉还是比较妙的,复杂度看上去很高(其实也很高),但是因为n只有100,所以还是可以过的. 考虑一个很暴力的状态f[i][j][x][y]表示考虑取区间i ~ j的方格,左右 ...

随机推荐

  1. 通过I2C总线向EEPROM中写入数据,记录开机次数

    没买板子之前,用protues画过电路图,实现了通过i2c总线向EEPROM中写入和读出数据. 今天,在自己买的板子上面写关于i2c总线的程序,有个地方忘了延时,调程序的时候很蛋疼.下面说说我对I2c ...

  2. C++练习题

    1. 用面向对象的程序描述员工拥有的股票,股票有公司,价格,数量属性,且拥有展现基本数据,更新价格,买进,卖出操作,并具有比较两个股票对象股值大小的比较方法. 2. 用面向对象的程序描述一个栈的操作, ...

  3. [JavaScript] js获取Html元素的实际宽度高度

    第一种情况就是宽高都写在样式表里,就比如#div1{width:120px;}.这中情况通 过#div1.style.width拿不到宽度,而通过#div1.offsetWidth才可以获取到宽度. ...

  4. JAVA File常用的API介绍

    package coreJava; import java.io.File; import java.io.IOException; public class FileDemo { public st ...

  5. no identities are available for signing

    原地址:http://www.cnblogs.com/imzzk/p/3501868.html 今天将做好的app提交到app store,结果就出现标题上的错误.“No identities are ...

  6. loadrunner http协议put模式脚本编写

    web_submit_data("rest", "Action=http://www.test.com/111ojhjh.do", "Method=P ...

  7. *[topcoder]TheTree

    http://community.topcoder.com/stat?c=problem_statement&pm=12746&rd=15703 这道题有意思.给了树的根和每层节点的个 ...

  8. 【大话QT之十】实现FTP断点续传(需要设置ftp服务器为“PASV”被动接收方式)

    应用需求: 网盘开发工作逐步进入各部分的整合阶段,当用户在客户端修改或新增加一个文件时,该文件要同步上传到服务器端对应的用户目录下,因此针对数据传输(即:上传.下载)这一块现在既定了三种传输方式,即: ...

  9. gzip [选项] 压缩(解压缩)

    减少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间.gzip是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用. 语法:gzip ...

  10. MySQL 授权详解

    (1)确认一下3306是否对外开放,mysql默认状态下是不开放对外访问功能的.查看的办法如下: 1 2 3 4 5 6 7 netstat -an | grep 3306 tcp        0  ...