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>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#define LL long long
#define _LL __int64
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1000000007;
const int maxn = 110;

int n,k;
int Map[maxn][maxn];
int Map1[maxn][maxn];
int Max;
int res;
int match[10010],chk[10010];
vector <int> ans;

int dfs(int p)
{
	for(int i = 1; i <= n; i++)
	{
		if(!chk[i] && Map1[p][i])
		{
			chk[i] = 1;
			if(match[i] == -1 || dfs(match[i]))
			{
				match[i] = p;
				return 1;
			}
		}
	}
	return 0;
}

int main()
{
	while(~scanf("%d %d",&n,&k) && (n || k))
	{
		ans.clear();
		Max = 0;
		for(int i = 1; i <= n; i++)
		{
			for(int j = 1; j <= n; j++)
			{
				scanf("%d",&Map[i][j]);
				if(Max < Map[i][j])
					Max = Map[i][j];
			}
		}
		for(int col = 1; col <= Max; col++) //对每种颜色的气球建二分图
		{
			memset(Map1,0,sizeof(Map1));
			for(int i = 1; i <= n; i++)
			{
				for(int j = 1; j <= n; j++)
				{
					if(Map[i][j] == col)
						Map1[i][j] = 1;
				}
			}

			memset(match,-1,sizeof(match));
			res = 0;
			for(int i = 1; i <= n; i++)
			{
				memset(chk,0,sizeof(chk));
				res += dfs(i);
			}
			if(res > k)
				ans.push_back(col);
		}

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

	}
	return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值