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.

 
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[55];//记录那种颜色出现过
int color;
/***********************二分匹配模板**************************/
const int MAXN=1000;
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=1;v<=n;v++)
        if(g[u][v]==color&&!used[v])
        //如果有这条边,并且这条边没有用过
        {
            used[v]=true;
            if(linker[v]==-1||dfs(color,linker[v]))//如果这个点没有匹配过,并且能找到匹配点,那么就可以以这个边作为匹配点
            {
                linker[v]=u;
                return true;
            }    
        }  
    return false;  
}    
int hungary(int color)//返回最大匹配数
{
    int res=0;
    int u;
    memset(linker,-1,sizeof(linker));
    for(u=1;u<=n;u++)
    {
        memset(used,0,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,0,sizeof g);
        memset(vis,0,sizeof vis);
        v.clear();
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                scanf("%d",&color);
                if(vis[color]==0){
                    vis[color]=1;
                }
                g[i][j]=color;
            }
        }
        
        for(int i=1;i<=50;i++){
            if(vis[i]){//这种颜色用过了
                if(hungary(i)>k)//说明这种颜色不可能被消掉
                    v.push_back(i);
            }
        }
        if(v.size()==0)
            printf("-1\n");
        else{
            for(int i=0;i<v.size();i++){
                printf(i==0?"%d":" %d",v[i]);
            }
            printf("\n");
        }
    }
    return 0;
}

 

posted @ 2016-12-19 22:33  勿忘初心0924  阅读(279)  评论(0编辑  收藏  举报