50 years, 50 colors HDU

来源:互联网 发布:阿里云服务器个人 编辑:程序博客网 时间:2024/05/01 11:01

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,501,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


题目大意:扎气球,每选择一种颜色的气球扎,每次只能选择把某一行或者某一列的该颜色的气球扎破,问有哪几种颜色的气球不能在k times 内全部扎破?

对于颜色k的每一个气球来说,如果位于(x,y)的气球被扎破,那么x行或者y列肯定被选择过,我们要找的就是是否在限定次数内,每一个该颜色的气球所在的x行和y列都被选择过。对于该颜色位于(x,y)的气球,我们连一条x到y的边,如果该边的任意一端被选择过,那么这个气球就被扎破了,所以我们要求的就是使所有边至少一端被选择了,也就是最小点覆盖是否比给定的times小就行了。

#include<iostream>#include<string.h>#include<string>#include<algorithm>#include<stdio.h>#include<vector>using namespace std;const int maxn=300;int n,m,k;int map[105][105];bool is[105];//vector<int> e[maxn];bool vis[maxn];int match[maxn];bool dfs(int v,int col){    for(int i=0;i<n;i++){        if(map[v][i]==col&&!vis[i]){            vis[i]=1;            if(match[i]<0||dfs(match[i],col)){                match[i]=v;                return 1;            }        }    }    return 0;}int bipart_match(int col){    int ans=0;    memset(match,-1,sizeof(match));    for(int i=0;i<n;i++){            memset(vis,0,sizeof(vis));            if(dfs(i,col))ans++;    }    return ans;}int main(){    int z,x,y;    while(scanf("%d%d",&n,&k),n+k){        vector<int> ans;        memset(is,0,sizeof(is));        for(int i=0;i<n;i++){            for(int j=0;j<n;j++){                scanf("%d",&map[i][j]);                is[map[i][j]]=1;            }        }        for(int i=1;i<=50;i++){            if(is[i]&&bipart_match(i)>k){                ans.push_back(i);            }        }        int ansn=ans.size();        if(ansn==0){printf("-1\n");}        else{            for(int i=0;i<ansn;i++){                printf("%d",ans[i]);                if(i<ansn-1)printf(" ");            }            printf("\n");        }    }    return 0;}
0 0