50 years, 50 colors HDU - 1498(最小点覆盖或者说最小顶点匹配)
On Octorber 21st, HDU 50-year-celebration, 50-color balloons floating around the campus, it’s so nice, isn’t it? To celebrate this meaningful day, the ACM team of HDU hold some fuuny games. Especially, there will be a game named “crashing color balloons”.
There will be a n*n matrix board on the ground, and each grid will have a color balloon in it.And the color of the ballon will be in the range of 1,501,50.After the referee shouts “go!”,you can begin to crash the balloons.Every time you can only choose one kind of balloon to crash, we define that the two balloons with the same color belong to the same kind.What’s more, each time you can only choose a single row or column of balloon, and crash the balloons that with the color you had chosen. Of course, a lot of students are waiting to play this game, so we just give every student k times to crash the balloons.
Here comes the problem: which kind of balloon is impossible to be all crashed by a student in k times.
Input
There will be multiple input cases.Each test case begins with two integers n, k. n is the number
of rows and columns of the balloons (1 <= n <= 100), and k is the times that ginving to each
student(0 < k <= n).Follow a matrix A of n*n, where Aij denote the color of the ballon in the i
row, j column.Input ends with n = k = 0.
Output
For each test case, print in ascending order all the colors of which are impossible to be crashed by a student in k times. If there is no choice, print “-1”.
Sample Input
1 1
1
2 1
1 1
1 2
2 1
1 2
2 2
5 4
1 2 3 4 5
2 3 4 5 1
3 4 5 1 2
4 5 1 2 3
5 1 2 3 4
3 3
50 50 50
50 50 50
50 50 50
0 0
Sample Output
-1
1
2
1 2 3 4 5
-1
//题意在刚接触的时候很难理解,和别人讨论过后才真正理解是什么意思。。。
//在给你一个n*n的矩阵之后,再给一个k代表你有几次机会,颜色相同的为一类气球,不同数字代表不同类气球
//对于描述中的最后一句话“ If there is no choice, print "-1". ”,有点不好理解,
//其实意思是对于每一类气球,你都有k次机会,如果在k次机会用完之后,看**哪一类的气球不能全部被弄破**
//然后把这些不能在k次机会全部弄破的气球的代表数字按照从小到大输出,如果每一类的气球都能在k次机会被弄破,
//那么则输出-1。
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<cstdio>
#include<sstream>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std;
const int INF=1e9+7;
const int maxn=510;
typedef long long ll;
int n,k;
int map[maxn][maxn];//存放整个图 map[i][j]的值 就是该方格的颜色
int link[maxn];
int color[maxn],vis[maxn];//color[i]为真 表示i号颜色在整个map中出现
int dfs(int i,int co)//本次dfs过程中 是用 第i行 颜色为 co 去匹配,
{
for(int j = 0; j < n; j ++)
{
if(!vis[j] && map[i][j] == co)
{
vis[j] = 1;
if(link[j] == -1 || dfs(link[j],co))
{
link[j] = i;//把点的编号记录下来
return 1;
}
}
}
return 0;
}
int KM(int co)
{
memset(link,-1,sizeof(link));//记录点的编号
int ans=0;
for(int i = 0; i < n ; i ++)
{
memset(vis,0,sizeof(vis));
if(dfs(i,co)) ans++;
}
return ans;
}
int main()
{
int i,j;
int ans[51],tol;//ans[] 存放的是要输出的不能消灭的颜色号,tol表示不能消灭的颜色数量
while(scanf("%d%d",&n,&k),n+k)
{
memset(color,0,sizeof(color));
memset(map,0,sizeof(map));
for(i = 0; i < n; i ++)
{
for(j = 0; j < n; j ++)
{
scanf("%d",&map[i][j]);
color[map[i][j]] = 1;
}
}
tol=0;
for(i = 1; i <= 50; i ++)
{
if(color[i])//注意这里表示 i 号颜色是不是有的
{
if(KM(i) > k)//如果匹配最小消除i号颜色用到的次数大于k 意思就是不能消灭
ans[tol++]=i;
}
}
sort(ans,ans+tol);//注意排序
if(tol == 0)
printf("-1");
else
for(i = 0; i < tol; i ++)
i == 0 ? printf("%d",ans[i]):printf(" %d",ans[i]);//这里是简单的格式控制,两数据中有空格
printf("\n");
}
return 0;
}
50 years, 50 colors HDU - 1498(最小点覆盖或者说最小顶点匹配)的更多相关文章
- nyoj237 游戏高手的烦恼(最小点覆盖)
题目237 题目信息 pid=237" style="text-decoration:none; color:rgb(55,119,188)">执行结果 本题排行 ...
- [poj] 3041 Asteroids || 最小点覆盖=最大二分图匹配
原题 本题为最小点覆盖,而最小点覆盖=最大二分图匹配 //最小点覆盖:用最少的点(左右两边集合的点)让每条边都至少和其中一个点关联. #include<cstdio> #include&l ...
- 二分图变种之最小路径覆盖、最小点覆盖集【poj3041】【poj2060】
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=54859604 向大(hei)佬(e)势力学(di ...
- LA 2038 Strategic game(最小点覆盖,树形dp,二分匹配)
题意即求一个最小顶点覆盖. 对于没有孤立点的图G=(V,E),最大独立集+最小顶点覆盖= V.(往最大独立集加点) 问题可以变成求树上的最大独立集合. 每个结点的选择和其父节点选不选有关, dp(u, ...
- Cogs 1632. 搬运工(二分图最小点覆盖)
搬运工 ★ 输入文件:worker.in 输出文件:worker.out 简单对比 时间限制:1 s 内存限制:256 MB [题目描述] 小涵向小宇推荐了一款小游戏. 游戏是这样的,在一个n*n的地 ...
- Strategic game POJ - 1463 【最小点覆盖集】
Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solu ...
- 牛客 216D 消消乐 (二分图最小点覆盖)
大意: 给定棋盘, 每次消除一行或一列, 求最小次数使得消除完所有'*'. 裸的二分图最小点覆盖. 二分图的最小点覆盖等于最大匹配, 输出方案时从所有左部未盖点开始标记交替路上的点, 最后左部所有未标 ...
- HDU 1498 50 years, 50 colors(最小点覆盖,坑称号)
50 years, 50 colors Problem Description On Octorber 21st, HDU 50-year-celebration, 50-color balloons ...
- hdu 1498 50 years, 50 colors 最小点覆盖
50 years, 50 colors Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
随机推荐
- bzoj1862/1056: [Zjoi2006]GameZ游戏排名系统
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1862 http://www.lydsy.com/JudgeOnline/problem.ph ...
- python学习笔记(五)数值类型和类型转换
Python中的数值类型有: 整型,如2,520 浮点型,如3.14159,1.5e10 布尔类型 True和False e记法: e记法即对应数学中的科学记数法 >>> 1.5e1 ...
- 集合框架源码学习之HashMap(JDK1.8)
目录: 0-1. 简介 0-2. 内部结构分析 0-2-1. JDK18之前 0-2-2. JDK18之后 0-3. LinkedList源码分析 0-3-1. 构造方法 0-3-2. put方法 0 ...
- mysql cast函数
CAST函数语法规则是:Cast(字段名 as 转换的类型 ),其中类型可以为: CHAR[(N)] 字符型 DATE 日期型DATETIME 日期和时间型DECIMAL float型SIGNED i ...
- Shell脚本中引用、调用另一个脚本文件的2种方法
Shell脚本中引用.调用另一个脚本文件的2种方法 http://www.jb51.net/article/67903.htm
- [ python ] 反射及item系列
反射 什么是反射? 通过字符串的形式操作对象相关属性.python中的事物都是对象: 关键方法: (1)getattr:获取属性 (2)setattr:设置属性 (3)hashattr:检测是否含有属 ...
- 生成RSA2公钥、私钥
RSA2是一种被使用广泛的非对称加密算法. openssl OpenSSL> genrsa -out app_private_key.pem # 私钥RSA2 OpenSSL> rsa - ...
- IntelliJ IDEA SpringBoot 使用第三方Tomcat以及部署
花了半天时间终于成功,记录以备查阅. 一.第三方Tomcat部署 部署部分参考的是:把spring-boot项目部署到tomcat容器中 目标:把spring-boot项目按照平常的web项目一样发布 ...
- hdu 2955(概率转化,01背包)
Hot~~招聘——巴卡斯(杭州),壹晨仟阳(杭州),英雄互娱(杭州) (包括2016级新生)除了校赛,还有什么途径可以申请加入ACM校队? Robberies Time Limit: 2000/100 ...
- HTTP资源合集
(1)MoZILLA开发者web技术文档之HTTP 未完待续...