50 years, 50 colors
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4027 Accepted Submission(s): 2216
Problem 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
题意:给你一个n*n的矩阵,在矩阵中分布着s种颜色的气球,给你k次扎破气球的操作,每次操作可以扎破一行,或一列的同一颜色的气球。问在k次操作后有那几种颜色的气球是不能被完全扎破的.
思路:枚举每种颜色判断一下该颜色的气球最少需要多少次才能全部扎破。
对于每一种颜色都是行列匹配,求最小点覆盖 看是否大于k 最小点覆盖 = 最大匹配
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 2555;
int used[maxn];
int link[maxn];
int mat[maxn][maxn];
int gn, gm;
int x[maxn], y[maxn];
int dfs(int t, int y) {
for(int i = 1; i <= gm; i++) {
if(!used[i] && mat[t][i] == y) {
used[i] = 1;
if(link[i] == -1 || dfs(link[i], y)) {
link[i] = t;
return 1;
}
}
}
return 0;
}
int maxmatch(int y) {
int num = 0;
memset(link, 0xff, sizeof(link));
for(int i = 1; i <= gn; i++) {
memset(used, 0, sizeof(used));
if(dfs(i, y)) {
num++;
}
}
return num;
}
int main()
{
int n, m, k, t, kcase = 0;
while(~scanf("%d %d", &n, &k) && n && k){
memset(mat, 0, sizeof(mat));
memset(x, 0, sizeof(x));
gn = n;
gm = n;
int a;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
scanf("%d", &a);
mat[i][j] = a;
x[a] = 1;
}
}
for(int i = 1; i <= 50; i++) {
if(x[i]) {
int res = maxmatch(i);
if(res <= k) {
x[i] = 0;
}
}
}
int flag = 1;
for(int i = 1; i <= 50; i++) {
if(x[i]) {
if(!flag) printf(" ");
printf("%d", i);
flag = 0;
}
}
if(!flag) puts("");
else puts("-1");
}
return 0;
}