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;
}