Question:
Here is a matrix of n * n, with s-colored balloons distributed in the matrix, and k bursts of balloons.
Each operation can break a line or a column of balloons of the same color. Q: k operations
The balloons of those colors cannot be completely broken.
Solution:
Use bipartite graph matching to find the maximum matching values for each color (the rows and columns are set A and Set B respectively; M [I, j] indicates A match ),
If the value is greater than k, "-1" is output; otherwise, the ascending sequence of colors is output.
Note: Find the least vertex in the bipartite graph, and associate each edge with at least one vertex. This is
The "minimum vertex overwrite" of a bipartite graph ".
[Csharp]
# Include "stdio. h"
# Include "string. h"
# Include "stdlib. h"
Int map [101] [101], mark [101];
Int v [101], link [101];
Int a [101], color [101];
Int k, n;
Int cmp (const void * a, const void * B)
{
Return * (int *) a-* (int *) B;
}
Int dfs (int I, int k)
{
Int j;
For (j = 1; j <= n; j ++)
{
If (map [I] [j] = k &&! V [j])
{
V [j] = 1;
If (link [j] = 0 | dfs (link [j], k ))
{
Link [j] = I;
Return 1;
}
}
}
Return 0;
}
Int main ()
{
Int I, j, t, tt, ans;
While (scanf ("% d", & n, & k )! =-1)
{
If (n = 0 & k = 0)
Break;
Memset (color, 0, sizeof (color ));
Memset (mark, 0, sizeof (mark ));
T = 0;
For (I = 1; I <= n; I ++)
{
For (j = 1; j <= n; j ++)
{
Scanf ("% d", & map [I] [j]);
If (! Mark [map [I] [j])
{
Mark [map [I] [j] = 1;
Color [t ++] = map [I] [j];
}
}
}
Tt = 0;
For (I = 0; I <t; I ++)
{
Ans = 0;
Memset (link, 0, sizeof (link ));
For (j = 1; j <= n; j ++)
{
Memset (v, 0, sizeof (v ));
If (dfs (j, color [I])
Ans ++;
}
If (ans> k)
A [tt ++] = color [I];
}
If (tt = 0)
Printf ("-1 \ n ");
Else www.2cto.com
{
Qsort (a, tt, sizeof (a [0]), cmp );
For (I = 0; I <TT-1; I ++)
Printf ("% d", a [I]);
Printf ("% d \ n", a [I]);
}
}
Return 0;
}
Author: yyf572132811