HDU 1498 50 years, 50 colors(二分图最小覆盖数)

本文深入探讨了游戏开发领域的关键技术,包括游戏引擎、编程语言、硬件优化等,并着重介绍了AI音视频处理在游戏中的应用,如语音识别、图像处理和AR特效,展示了技术如何提升游戏体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:给你一个n*n的矩阵,在矩阵中分布着s种颜色的气球,给你k次扎破气球的操作,每次操作可以扎破一行,或一列的同一颜色的气球。问在k次操作后有那几种颜色的气球是不能被完全扎破的.

思路只需要对于每种颜色判断一下该颜色的气球最少需要多少次才能全部扎破即可.

           假设当前处理颜色为1的气球(注意气球颜色最多有50). 我们把行标号放左点集,列标号放右点集合. 如果(i,j)格子的颜色为1,那么就连一条左i与右j点的边.

           由于我们每次可以消灭一行或一列的所有同色气球,那么我们只需要选出尽量少的行号或列号(即左右点集中的点),然后看看这些行号或列号是否正好已经把所有的1颜色气球都覆盖了.

       上述问题就是二分图的最小覆盖数问题. 最小覆盖数= 最大匹配数.


#include<cstdio>
#include<cstring>
#include<set>
#include<vector>
using namespace std;
const int maxn=100+5;

struct Max_Match
{
    int n,m;//左右点集大小,点从1开始编号
    int g[maxn][maxn];//g[i]表示左边第i个点邻接的右边点的集合
    bool vis[maxn];//vis[i]表示右边第i个点是否在本次match中被访问过
    int left[maxn];//left[i]==j表右边第i个点与左边第j个点匹配,为-1表无点匹配
    int color;
    void init(int n)
    {
        this->n=n;
       // this->m=m;
      // for(int i=1;i<=n;i++) g[i].clear();
        memset(left,-1,sizeof(left));
    }

    //判断从左u点是否可以找到一条增广路
    bool match(int u)
    {
        for(int v=1;v<=n;v++)
        {
            if(!vis[v] && g[u][v]==color)
            {
                vis[v]=true;
                if(left[v]==-1 || match(left[v]))//找到增广路
                {
                    left[v]=u;
                    return true;
                }
            }
        }
        return false;
    }

    //返回当前二分图的最大匹配数
    int solve(int c)
    {
		color = c;
        int ans=0;//最大匹配数
        for(int i=1;i<=n;i++)//每个左边的节点找一次增广路
        {
            memset(vis,0,sizeof(vis));
            if(match(i)) ans++;//找到一条增广路,形成一个新匹配
        }
        return ans;
    }
}MM;

int main()
{
	int n,k;
	while (scanf("%d%d",&n,&k)!=EOF && n)
	{
		set<int>color;
		MM.init(n);
		for (int i = 1;i<=n;i++)
			for (int j = 1;j<=n;j++)
			{
				scanf("%d",&MM.g[i][j]);
				color.insert(MM.g[i][j]);
			}

		int ans = 0;
		vector<int>col;
		for (set<int>::iterator it = color.begin();it!=color.end();it++)
		{
			memset(MM.left,-1,sizeof(MM.left));
			if (MM.solve(*it) > k)
			{
				++ans;
				col.push_back(*it);
			}
		}

		if (col.size()==0)
			printf("-1\n");
		else
		{
			for (int i = 0;i<col.size()-1;i++)
				printf("%d ",col[i]);
			printf("%d\n",col[col.size()-1]);
		}
	}
}

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
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值