http://acm.hdu.edu.cn/showproblem.php?pid=1498

题意:给出一个 n*n 的矩阵,里面的数字代表一种颜色,每次能炸掉一排或者一列的相同颜色的气球,问有哪些颜色的气球不能在 k 次内炸完的,从小到大输出,能炸完输出-1.

思路:先存下点,用一个数字标记颜色是否出现过,然后遍历每一种颜色,再把这种颜色的行号和列号连边,跑一遍匈牙利,如果得到的结果大于 k 那么这种颜色就不行。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <cstdlib>
  5. #include <algorithm>
  6. #include <string>
  7. #include <iostream>
  8. #include <stack>
  9. #include <map>
  10. #include <queue>
  11. using namespace std;
  12. #define N 100010
  13. #define INF 0x3f3f3f3f
  14. struct node
  15. {
  16. int nxt, v;
  17. }edge[*];
  18. int mp[][];
  19. int head[], tot;
  20. bool vis[], col[];
  21. int ans[];
  22. int match[], n, k;
  23.  
  24. void add(int u, int v)
  25. {
  26. edge[tot].v = v; edge[tot].nxt = head[u]; head[u] = tot++;
  27. }
  28.  
  29. bool dfs(int u)
  30. {
  31. for(int i = head[u]; ~i; i = edge[i].nxt) {
  32. int v = edge[i].v;
  33. if(!vis[v]) {
  34. vis[v] = ;
  35. if(match[v] == - || dfs(match[v])) {
  36. match[v] = u;
  37. return true;
  38. }
  39. }
  40. }
  41. return false;
  42. }
  43.  
  44. int main()
  45. {
  46. while(scanf("%d%d", &n, &k), n + k) {
  47. memset(mp, , sizeof(mp));
  48. memset(col, , sizeof(col));
  49. for(int i = ; i <= n; i++) {
  50. for(int j = ; j <= n; j++) {
  51. scanf("%d", &mp[i][j]);
  52. col[mp[i][j]] = ;
  53. }
  54. }
  55. int l = ;
  56. for(int i = ; i <= ; i++) {
  57. if(col[i]) {
  58. tot = ;
  59. memset(head, -, sizeof(head));
  60. for(int j = ; j <= n; j++) {
  61. for(int k = ; k <= n; k++) {
  62. if(mp[j][k] == i) {
  63. add(j, k);
  64. }
  65. }
  66. }
  67. memset(match, -, sizeof(match));
  68. int tmp = ;
  69. for(int j = ; j <= n; j++) {
  70. memset(vis, , sizeof(vis));
  71. if(dfs(j)) tmp++;
  72. }
  73. if(tmp > k) ans[l++] = i;
  74. }
  75. }
  76. // printf("ans : ");
  77. for(int i = ; i < l; i++) {
  78. if(i != l - ) printf("%d ", ans[i]);
  79. else printf("%d\n", ans[i]);
  80. }
  81. if(l == ) printf("-1\n");
  82. }
  83. return ;
  84. }

HDU 1498:50 years, 50 colors(二分图匹配)的更多相关文章

  1. HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))

    Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  2. [ACM] HDU 3395 Special Fish (最大重量二分图匹配,KM算法)

    Special Fish Problem Description There is a kind of special fish in the East Lake where is closed to ...

  3. Hdu 3289 Rain on your Parade (二分图匹配 Hopcroft-Karp)

    题目链接: Hdu 3289 Rain on your Parade 题目描述: 有n个客人,m把雨伞,在t秒之后将会下雨,给出每个客人的坐标和每秒行走的距离,以及雨伞的位置,问t秒后最多有几个客人可 ...

  4. 【HDU 1150】Machine Schedule(二分图匹配)

    机器的不同模式为点,对于每个job,建两条边 A机器需要的模式<->B机器需要的模式. 问题转化为最小点覆盖,然后用二分图的最小点覆盖==最大匹配,用匈牙利算法解. #include &l ...

  5. Hdu 5285 wyh2000 and pupil (bfs染色判断奇环) (二分图匹配)

    题目链接: BestCoder Round #48 ($) 1002 题目描述: n个小朋友要被分成两班,但是有些小朋友之间是不认得的,所以规定不能把不认识的小朋友分在一个班级里面,并且一班的人数要比 ...

  6. hdu 1498 50 years, 50 colors(二分匹配_匈牙利算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1498 50 years, 50 colors Time Limit: 2000/1000 MS (Ja ...

  7. HDU——1498 50 years, 50 colors

    50 years, 50 colors Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  8. HDU 1498 50 years, 50 colors(最小点覆盖,坑称号)

    50 years, 50 colors Problem Description On Octorber 21st, HDU 50-year-celebration, 50-color balloons ...

  9. hdu 1498 50 years, 50 colors 最小点覆盖

    50 years, 50 colors Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  10. 50 years, 50 colors

    50 years, 50 colors Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. C++内存未释放的情况

    以下例子中,存储了整数123的记亿体空间不能被删除,因为地址丢失了.这些空间已无法再使用. #include <iostream> using namespace std; int mai ...

  2. IDisplayTransformation

    IDisplayTransformation Bounds Full extent in world coordinates. The Bounds property controls the ful ...

  3. [ArcEngine]Geotransformation地理变换

    Geotransformation 地理变换 The Abridged Molodensky transformation is a three parameter transformation三参 ...

  4. iOS 设置导航栏之二(设置导航栏的颜色、文字的颜色、左边按钮的文字及颜色)

                      #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicati ...

  5. 【转】Java多线程编程中易混淆的3个关键字( volatile、ThreadLocal、synchronized)总结

    概述 最近在看<ThinKing In Java>,看到多线程章节时觉得有一些概念比较容易混淆有必要总结一下,虽然都不是新的东西,不过还是蛮重要,很基本的,在开发或阅读源码中经常会遇到,在 ...

  6. CSS位置如何获取的

  7. struts 2.0部署

    环境:linux centos 64位. 1)下载JDK6.0,具体文件名是:jdk-6u45-linux-x64.bin 安装:chmod 755 jdk*.bin ./jdk....bin 设置环 ...

  8. C#删除xml中某个节点的子节点方法

    if (File.Exists(xmlFilePath)) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlFilePath); Xm ...

  9. C++之路进阶——优先队列优化最短路径算法(dijkstra)

    一般的dijkstra算法利用贪心的思想,每次找出最短边,然后优化到其他点的的距离,我们还采用贪心思路,但在寻找最短边进行优化,之前是双重for循环,现在我们用优先队列来实现. 代码解释: //样例程 ...

  10. CSS3 filter:drop-shadow滤镜与box-shadow区别应用 抄的

    CSS3 filter:drop-shadow滤镜与box-shadow区别应用 这篇文章发布于 2016年05月18日,星期三,01:07,归类于 css相关. 阅读 5777 次, 今日 12 次 ...