HDU 1498 50 years, 50 colors(最小点覆盖,坑题)

50 years, 50 colors

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1635    Accepted Submission(s): 892

Problem Description

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, 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

有这样一个矩阵,矩阵里面是气球,气球有颜色编号1-50,询问在k次之内能不能把同种颜色存在的气球消掉(每次询问有K次机会),超过K此的气球编号,储存起来,并按编号按升序输出。

题意不是很好理解,但是看明白之后,就很清晰了

求最小点覆盖——即 用最少的点覆盖图中最多的边。

依次枚举就可以

矩阵的一维下标 当做 X集合,二维下标当做Y集合,询问所有点的最大匹配,比K大自然不满足

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <math.h>
#define init(a) memset(a,0,sizeof(a))
#define PI acos(-1,0)
using namespace std;
const int maxn = 110;
const int maxm = 100001;
#define lson left, m, id<<1
#define rson m+1, right, id<<1|1
#define min(a,b) (a>b)?b:a
#define max(a,b) (a>b)?a:b
const int N = 50010;
int ma[maxn][maxn];
int line[maxn],res[maxn];
bool vis[maxn],bj[maxn];
int k,n,m,t;
int cmp(const void *a,const void *b)
{
    return *(int *)a - *(int *)b;
}
int DFS(int st,int u)
{
    for(int v = 1;v<=n;v++)
    {
        if(ma[u][v]== st && !vis[v])
        {
            vis[v] = 1;
            if(line[v]==-1 || DFS(st,line[v]))
            {
                line[v] = u;
                return 1;
            }
        }
    }
    return 0;
}
int K_M(int st)
{
    memset(line,-1,sizeof(line));
    int ans = 0;
    for(int i = 1;i<=n;i++)
    {
        init(vis);
        ans += DFS(st,i);
    }
    return ans;
}
int main()
{
    int t;
    while(~scanf("%d%d",&n,&k))
    {
        if(n==0 && k== 0) break;
        init(ma); init(res); init(bj);
        for(int i = 1;i<=n;i++)
        {
            for(int j = 1;j<=n;j++)
            {
                scanf("%d",&t);
                ma[i][j] = t;
                if(!bj[t])
                    bj[t] = 1;
            }
        }
        int cnt,num = 0;
        for(int i = 1;i<=55;i++)
        {
            if(bj[i])
            {
                 cnt = K_M(i);
                 (cnt>k)?(res[num++] = i):(1);
            }
        }
        if(num==0) puts("-1");
        else
        {
            //sort(res,res+num);
            qsort(res,num,sizeof(res[0]),cmp);
            for(int j = 0;j<num-1;j++)
                printf("%d ",res[j]);
            printf("%d\n",res[num-1]);
        }
    }
    return 0;
}

HDU 1498 50 years, 50 colors(最小点覆盖,坑题),布布扣,bubuko.com

时间: 2024-10-17 23:54:17

HDU 1498 50 years, 50 colors(最小点覆盖,坑题)的相关文章

HDU 3360 National Treasures 奇偶匹配最小点覆盖

题目来源:HDU 3360 National Treasures 题意:如果a[i][j] != -1 把他转成二进制 最多有12位 代表题目那张图的12个位置 如果对应位是1 说明在那里放一个守卫可以看住a[i][j]位置上的这个东西 思路:明显死最小点覆盖 奇偶匹配建图 #include <cstdio> #include <cstring> #include <vector> using namespace std; const int maxn = 55; in

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

50 years, 50 colors Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description On Octorber 21st, HDU 50-year-celebration, 50-color balloons floating around the campus, it's so nice, isn't it? To celebrate

hdu1498 50 years, 50 colors --- 最小点覆盖

给一个矩阵,里面有一些不同颜色的气球.每次能够消灭一行或一列中某一种颜色的气球,问你在k次及以内,有哪些颜色的气球是不管怎样也消不完的. 那么思路就是,对每一种颜色的气球求最小点覆盖.>k 则为答案. 相当于 poj3041的加强版,由于矩阵中不是每个点都是等价的. #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm>

hdu 1151 或 poj 1422 二分图 最小点覆盖集

最小点覆盖集的裸题,只要“拆点建边”然后求出最大匹配,则:最小点覆盖集的大小 = 点数 - 最大匹配 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int N = 121; 7 const int M = 5000; 8 bool visit[N]; 9 int mark[N]; 10 int head[N]; 11 int

hdu - 1150 Machine Schedule (二分图匹配最小点覆盖)

http://acm.hdu.edu.cn/showproblem.php?pid=1150 有两种机器,A机器有n种模式,B机器有m种模式,现在有k个任务需要执行,没切换一个任务机器就需要重启一次, 如果任务i在机器A上执行,A机器需要一个对应的模式A,如果在机器B上执行,机器A需要一个模式B. 一直就是机器A在切换模式,现在让你安排一种合理的任务执行顺序,让机器重启次数最少. 每个任务之间连一条边.二分图的最小顶点覆盖就是求最少的点可以连接所有的边,然后转化成最大匹配即可. 这题就是初始状态

poj 3041Asteroids 二分匹配求最小点覆盖模板题

//最大匹配=最小覆盖 //这题是求最小覆盖点的模板题 #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn = 510; int line[maxn][maxn]; int match[maxn]; int vis[maxn]; int N , K; int find(int start) { for(int i = 1;i <=  N;

HNU 2014 Warm Up 15 E Easy Delete 最小点覆盖=最大匹配数

题目链接:点击打开链接 Easy Delete Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 8, Accepted users: 4 Problem 13103 : No special judgement Problem description huicpc0215 has downloaded a lots of files in his desktop. Si

HDU 1498 50 years, 50 colors(二分最大匹配之最小点覆盖)

题目地址:HDU 1498 晕啊...三个人同时做的这个题,结果全都理解错意思了..而且每个人理解错的地方还都不一样..但是样例还都能过,...简直炫酷... 题意:n*n的矩阵放置不同的颜色(不同的数字代表不同的颜色),你有k次选择,每一次只能选择某一行或某一列,可以消除该行(列)的所有颜色,问有哪几种颜色,无论怎样经过k次选择后依然无法完全抹去. 这个题的思路就是分别求出每种颜色的最少操作次数.然后只要大于k次的就是不符合要求的.然后输出就行了. #include <iostream> #

hdu 50 years, 50 colors(枚举点,最小点覆盖)

http://acm.hdu.edu.cn/showproblem.php?pid=1498 大致题意:给一个n*n的格子,每个格子中都放有不同颜色的气球.每次你可以选择一行或一列以及一种颜色的气球,然后将该行或该列上该种颜色的气球全部扎破.问经过K次,会有哪些气球是不可能被完全扎破的,按升序输出. 以行列为X,Y集合,对每一种颜色的气球构建二分图,求出二分图的最小点覆盖(最大匹配)m,即选取最少的点将所有的边覆盖.若m>k说明该颜色的气球不可能被完全扎破. #include <stdio.h