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

50 years, 50 colors



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
",res[num-1]);
        }
    }
    return 0;
}


 
原文地址:https://www.cnblogs.com/blfshiye/p/5042705.html