50 years, 50 colors

时间:2023-03-09 13:31:34
50 years, 50 colors

50 years, 50 colors

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 137 Accepted Submission(s): 86
 
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.

50 years, 50 colors

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
Author
8600
Source
“2006校园文化活动月”之“校庆杯”大学生程序设计竞赛暨杭州电子科技大学第四届大学生程序设计竞赛
Recommend
LL
/*
给你一个n*n的格子矩阵每个格子中都存放着一种颜色的气球,现在你有k次机会,每次选择一中颜色,然后在选择一行,或者一列,将这行或这列的这种颜色
的气球全部踩破,k次操作之后将没有踩破的气球安照从小到大的顺序输出,如果没有就输出-1 初次思路:将颜色和边转化为二分图,然后进行二分图匹配,求最小覆盖点,也就是最大匹配,每种颜色的最大匹配的边数
假设所有的边选取的k次的边都是行,或者都是列 */
#include<bits/stdc++.h>
using namespace std;
int n,k;
int vis[];//记录那种颜色出现过
int color;
/***********************二分匹配模板**************************/
const int MAXN=;
int g[MAXN][MAXN];//编号是0~n-1的
int linker[MAXN];//记录匹配点i的匹配点是谁
vector<int>v;
bool used[MAXN];
bool dfs(int color,int u)//回溯看能不能通过分手来进行匹配
{
int v;
for(v=;v<=n;v++)
if(g[u][v]==color&&!used[v])
//如果有这条边,并且这条边没有用过
{
used[v]=true;
if(linker[v]==-||dfs(color,linker[v]))//如果这个点没有匹配过,并且能找到匹配点,那么就可以以这个边作为匹配点
{
linker[v]=u;
return true;
}
}
return false;
}
int hungary(int color)//返回最大匹配数
{
int res=;
int u;
memset(linker,-,sizeof(linker));
for(u=;u<=n;u++)
{
memset(used,,sizeof(used));
if(dfs(color,u))//如果这个点有匹配点
res++;
}
return res;
}
/***********************二分匹配模板**************************/
int main(){
//freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&k)!=EOF&&(n+k)){
memset(g,,sizeof g);
memset(vis,,sizeof vis);
v.clear();
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&color);
if(vis[color]==){
vis[color]=;
}
g[i][j]=color;
}
} for(int i=;i<=;i++){
if(vis[i]){//这种颜色用过了
if(hungary(i)>k)//说明这种颜色不可能被消掉
v.push_back(i);
}
}
if(v.size()==)
printf("-1\n");
else{
for(int i=;i<v.size();i++){
printf(i==?"%d":" %d",v[i]);
}
printf("\n");
}
}
return ;
}