1. 1 //hdu1507 挺不错的题
  2. #include<stdio.h>
  3. #include<string.h>
  4. #define INF 99999999
  5. struct node
  6. {
  7. int x;
  8. int y;
  9. }rem[];
  10. struct Node
  11. {
  12. int v;
  13. int next;
  14. }edge[];
  15. int n,m,num,vis[],match[],map[][],mmark[],index,pre[];
  16. void add(int x,int y)
  17. {
  18. edge[index].v=y;
  19. edge[index].next=pre[x];
  20. pre[x]=index++;
  21. }
  22. void build(int x,int y,int a,int b)
  23. {
  24. int i,j;
  25. if(a<=||a>n||b<=||b>m||map[a][b]==)
  26. return ;
  27. else add(map[x][y],map[a][b]);
  28. }
  29. void test(int x,int y)
  30. {
  31. build(x,y,x-,y);
  32. build(x,y,x+,y);
  33. build(x,y,x,y-);
  34. build(x,y,x,y+);
  35. }
  36. void makemap()
  37. {
  38. int i,j;
  39. for(i=;i<=n;i++)
  40. for(j=;j<=m;j++)
  41. {
  42. if(map[i][j]==)
  43. continue;
  44. test(i,j);
  45. }
  46. }
  47. int dfs(int u)
  48. {
  49. int i,j;
  50. for(i=pre[u];i!=-;i=edge[i].next)
  51. {
  52. int j=edge[i].v;
  53. if(!vis[j])
  54. {
  55. vis[j]=;
  56. if(match[j]==-||dfs(match[j]))
  57. {
  58. match[j]=u;
  59. return ;
  60. }
  61. }
  62. }
  63. return ;
  64. }
  65. int main()
  66. {
  67. int i,j,k;
  68. while(scanf("%d%d",&n,&m)!=EOF)
  69. {
  70. if(!n&&!m)break;
  71. scanf("%d",&k);
  72. memset(mmark,,sizeof(mmark));
  73. for(i=;i<=n;i++)
  74. for(j=;j<=m;j++)
  75. map[i][j]=INF;
  76.  
  77. for(i=;i<k;i++)
  78. {
  79. int x,y;
  80. scanf("%d%d",&x,&y);
  81. map[x][y]=;
  82. }
  83.  
  84. num=;
  85. for(i=;i<=n;i++)
  86. for(j=;j<=m;j++)
  87. {
  88. if(map[i][j])
  89. {
  90. map[i][j]=num;
  91. rem[num].x=i;
  92. rem[num++].y=j;
  93. }
  94. }
  95. /*for(i=1;i<=n;i++)
  96. {
  97. for(j=1;j<=m;j++)
  98. printf("%d ",map[i][j]);
  99. printf("\n");
  100. }*/
  101. index=;
  102. memset(pre,-,sizeof(pre));
  103. makemap();
  104. /*for(i=1;i<num;i++)
  105. {
  106. for(j=1;j<num;j++)
  107. {
  108. printf("%d ",g[i][j]);
  109. }
  110. printf("\n");
  111. }*/
  112. memset(match,-,sizeof(match));
  113. int ans=;
  114. for(i=;i<num;i++)
  115. {
  116. memset(vis,,sizeof(vis));
  117. if(dfs(i))
  118. ans++;
  119. }
  120. int mark[];
  121. memset(mark,,sizeof(mark));
  122. printf("%d\n",ans/);
  123. for(i=;i<num;i++)
  124. {
  125. if(!mark[i]&&!mark[match[i]]&&match[i]!=-)
  126. {
  127. printf("(%d,%d)--(%d,%d)\n",rem[i].x,rem[i].y,rem[match[i]].x,rem[match[i]].y);
  128. mark[i]=;mark[match[i]]=;
  129. }
  130. }
  131. printf("\n");
  132. }
  133. }
  134.  
  135. /*
  136.  
  137. 3 3
  138. 4
  139. 2 3
  140. 1 3
  141. 3 2
  142. 1 2
  143. 4 5
  144. 6
  145. 1 2
  146. 1 3
  147. 3 5
  148. 3 4
  149. 4 2
  150. 2 5
  151.  
  152. */

hdu1507二分匹配的更多相关文章

  1. POJ 1274 The Perfect Stall、HDU 2063 过山车(最大流做二分匹配)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24081   Accepted: 106 ...

  2. [kuangbin带你飞]专题十 匹配问题 二分匹配部分

    刚回到家 开了二分匹配专题 手握xyl模板 奋力写写写 终于写完了一群模板题 A hdu1045 对这个图进行 行列的重写 给每个位置赋予新的行列 使不能相互打到的位置 拥有不同的行与列 然后左行右列 ...

  3. BZOJ 1189 二分匹配 || 最大流

    1189: [HNOI2007]紧急疏散evacuate Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1155  Solved: 420[Submi ...

  4. Kingdom of Obsession---hdu5943(二分匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5943 题意:给你两个数n, s 然后让你判断是否存在(s+1, s+2, s+3, ... , s+n ...

  5. poj 2060 Taxi Cab Scheme (二分匹配)

    Taxi Cab Scheme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5710   Accepted: 2393 D ...

  6. [ACM_图论] Sorting Slides(挑选幻灯片,二分匹配,中等)

    Description Professor Clumsey is going to give an important talk this afternoon. Unfortunately, he i ...

  7. [ACM_图论] The Perfect Stall 完美的牛栏(匈牙利算法、最大二分匹配)

    描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来:每头奶牛都只愿意在她们 ...

  8. nyoj 237 游戏高手的烦恼 二分匹配--最小点覆盖

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=237 二分匹配--最小点覆盖模板题 Tips:用邻接矩阵超时,用数组模拟邻接表WA,暂时只 ...

  9. UVA5874 Social Holidaying 二分匹配

    二分匹配简单题,看懂题意,建图比较重要. #include<stdio.h> #include<string.h> #define maxn 1100 int map[maxn ...

随机推荐

  1. codeforces 713B B. Searching Rectangles(二分)

    题目链接: B. Searching Rectangles time limit per test 1 second memory limit per test 256 megabytes input ...

  2. POJ 1269 Intersecting Lines【判断直线相交】

    题意:给两条直线,判断相交,重合或者平行 思路:判断重合可以用叉积,平行用斜率,其他情况即为相交. 求交点: 这里也用到叉积的原理.假设交点为p0(x0,y0).则有: (p1-p0)X(p2-p0) ...

  3. 移动App崩溃测试用例设计

    我们的日常生活中对移动设备越来越多的使用意味着移动App测试这个主题已成为需要考虑的一个无法避免的问题.根据最近的调查研究,用户难以容忍有bug的移动App. 移动App Bug的影响是用户体验差.A ...

  4. Android开发eclipse运行程序时报timeout的解决方法

    eclipse开发Android程序时,忽然莫名其妙报这个错,之前还好好的.忽然就不行了. Failed to install xxx.apk on device~~~: timeout 尝试过手机里 ...

  5. 自定义WPF ListBox的选中项样式

    首先介绍一种简单地方法:就是通过自定义SystemColors类的参数来自定义WPF ListBox选择颜色的,SystemColors的HighlightBrushKey和HighlightText ...

  6. linux patch 格式与说明(收录)

    转:http://blog.chinaunix.net/uid-26813001-id-3282954.html 首先介绍一下diff和patch.在这里不会把man在线文档上所有的选项都介绍一下,那 ...

  7. MySQL数据备份小结

    一 MySQL备份恢复总结: 1,备份所有库 2,分库备份 3,备份某库中的某表 4,备份某库中的多个表 5,分表备份 6,只备份表结构 7,只备份数据 二 MySQL备份恢复参数总结: -A 备份所 ...

  8. 将Log4net的配置配置到的独立文件中

    本文转载:http://blog.csdn.net/wanzhuan2010/article/details/7587780 另外一篇博客:http://grim1980.blog.sohu.com/ ...

  9. An Introduction to Interactive Programming in Python (Part 1) -- Week 2_3 练习

    Mini-project description - Rock-paper-scissors-lizard-Spock Rock-paper-scissors is a hand game that ...

  10. JS 之DOM对象(2)

    http://www.cnblogs.com/zourong/p/4792394.html 这篇文件介绍了DOM1中的一些属性和方法,下面的内容主要介绍DOM2和DOM3中新增的内容. 框架的变化 框 ...