Problem Description
On Octorber 21st, HDU 50-year-celebration,50-color balloons floating around the campus, it's so nice, isn't it? Tocelebrate 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 colorballoon in it.And the color of the ballon will be in the range of [1, 50].Afterthe referee shouts "go!",you can begin to crash the balloons.Everytime you can only choose one kind of balloon to crash, we define that the twoballoons with the same color belong to the same kind.What's more, each time youcan only choose a single row or column of balloon, and crash the balloons thatwith the color you had chosen. Of course, a lot of students are waiting to playthis 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 crashedby a student in k times.
Input
Therewill be multiple input cases.Each test case begins with two integers n, k. n isthe number of rows and columns of the balloons (1 <= n <= 100), and k isthe times that ginving to each student(0 < k <= n).Follow a matrix A ofn*n, where Aij denote the color of the ballon in the i row, j column.Input endswith n = k = 0.
Output
Foreach test case, print in ascending order all the colors of which are impossibleto 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
—————————————————————————————————
行列建图法,用暴力枚举(才50种颜色而已)求出每种颜色的最大匹配。
_________________________________________________________
#include<iostream>
using namespace std;
int g[105][105],clo[55];
#define MAX 105
int vis[MAX] ;
int nx,ny;
int map[MAX][MAX];
int dx[MAX],dy[MAX];//都初始化为-1
int DFS(int v)
{
for(int i=0;i<ny;i++)
if(map[v][i]&&!vis[i])
{
vis[i]=1;
if(dy[i]==-1||DFS(dy[i]))
{
dx[v]=i;
dy[i]=v;
return 1;
}
}
return 0;
}
int Hungary()
{
int ans=0;
memset(dx,0xff,sizeof(dx));
memset(dy,0xff,sizeof(dy));
for(int i=0;i<nx;i++)
if(dx[i]==-1)
{
memset(vis,0,sizeof(vis));
ans+=DFS(i);
}
return ans;
}
int main()
{
int p,n,k,i,j,l,ac[1005],kk;
while(~scanf("%d%d",&n,&k))
{
p=0;
memset(clo,0,sizeof(clo));
if(n==0&&k==0)
break;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
scanf("%d",&g[i][j]);
clo[g[i][j]]=1;
}
for(i=1;i<=50;i++)
{
if(clo[i])
{
int pp=i;
memset(map,0,sizeof(map));
for(j=0;j<n;j++)
for(kk=0;kk<n;kk++)
{
if(g[j][kk]==pp)
map[j][kk]=1;
}
nx=ny=n;
int re;
re=Hungary();
if(re>k)
ac[p++]=i;
}
}
if(p==0)
printf("-1\n");
else
{
for(i=0;i<p-1;i++)
printf("%d ",ac[i]);
printf("%d\n",ac[p-1]);
}
}
}