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

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

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

 #include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <iostream>
#include <stack>
#include <map>
#include <queue>
using namespace std;
#define N 100010
#define INF 0x3f3f3f3f
struct node
{
int nxt, v;
}edge[*];
int mp[][];
int head[], tot;
bool vis[], col[];
int ans[];
int match[], n, k; void add(int u, int v)
{
edge[tot].v = v; edge[tot].nxt = head[u]; head[u] = tot++;
} bool dfs(int u)
{
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if(!vis[v]) {
vis[v] = ;
if(match[v] == - || dfs(match[v])) {
match[v] = u;
return true;
}
}
}
return false;
} int main()
{
while(scanf("%d%d", &n, &k), n + k) {
memset(mp, , sizeof(mp));
memset(col, , sizeof(col));
for(int i = ; i <= n; i++) {
for(int j = ; j <= n; j++) {
scanf("%d", &mp[i][j]);
col[mp[i][j]] = ;
}
}
int l = ;
for(int i = ; i <= ; i++) {
if(col[i]) {
tot = ;
memset(head, -, sizeof(head));
for(int j = ; j <= n; j++) {
for(int k = ; k <= n; k++) {
if(mp[j][k] == i) {
add(j, k);
}
}
}
memset(match, -, sizeof(match));
int tmp = ;
for(int j = ; j <= n; j++) {
memset(vis, , sizeof(vis));
if(dfs(j)) tmp++;
}
if(tmp > k) ans[l++] = i;
}
}
// printf("ans : ");
for(int i = ; i < l; i++) {
if(i != l - ) printf("%d ", ans[i]);
else printf("%d\n", ans[i]);
}
if(l == ) printf("-1\n");
}
return ;
}

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. Android ListView 自定义 Adapter

    自定义Adapter类 public class ListViewAdapter extends BaseAdapter { private static final String TAG = Mai ...

  2. Maven开发环境的搭建,含jetty调试[简单明了]

    之前的一片老文,搬过来. 1. Maven的开发环境需要哪些部件a) Maven.去http://maven.apache.org/ 下载.将其bin目录加入进系统PATH.测试方法:用命令mvn – ...

  3. .NET Framework 4 中的并行编程9---线程安全集合类

    原文转载自:http://www.cnblogs.com/xray2005/archive/2011/10/11/2206745.html 在.Net 4中,新增System.Collections. ...

  4. Hadoop2.2.0 第一步完成MapReduce wordcount计算文本数量

    1.完成Hadoop2.2.0单机版环境搭建之后需要利用一个例子程序来检验hadoop2 的mapreduce的功能 //启动hdfs和yarn sbin/start-dfs.sh sbin/star ...

  5. jQuery 扩展功能

    源码如下: /*! * 说明:Jquery库扩展 * 创建时间: leo 2016/10/13 */ (function (window, jQuery, undefined) { jQuery.ex ...

  6. PostgreSQL数据库中跨库访问解决方案

    PostgreSQL跨库访问有3种方法:Schema,dblink,postgres_fdw. 方法A:在PG上建立不同SCHEMA,将数据和存储过程分别放到不同的schema上,经过权限管理后进行访 ...

  7. Lintcode: Segment Tree Build

    The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...

  8. <s:iterator> 对list操作的一种方法

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA6IAAAH3CAIAAAAuRnW9AAAgAElEQVR4nOzdf4gk1333+wLDDffhPp

  9. 关于C语言链表的学习

    今天讲了一种非传统型的链表.听得不是太好. 到数据结构那一部分的时候.一定要好好听听.

  10. bootstrap, boosting, bagging 几种方法的联系

    http://blog.csdn.net/jlei_apple/article/details/8168856 这两天在看关于boosting算法时,看到一篇不错的文章讲bootstrap, jack ...