50 years, 50 colors (HDU-1498)

题目描述:

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^2 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.

image

撞气球游戏,一个n*n的矩阵中,有不同颜色的气球,气球的颜色最多50种(从1到50)。
游戏开始前,先选择一种颜色。游戏开始后,每次选择一行或者一列包含该种颜色的气球进行撞击。如果选择行,那么这一行的气球都会炸裂。如果选择列,这一列的气球都炸裂。
请你求出,有多少种颜色的气球,无论怎么玩,都不能在K次之内,把所有同色的气球都撞裂。

输入

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**.

有多组数据
每组测试数据,第一行两个整数n和k
接下来n行,每行n个整数,表示该行的气球颜色。
n=k=0时读入结束。

输出

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".

按照升序输出哪些颜色的气球无论怎么玩,都不能在K次之内,把所有同色的气球都撞裂,如果没有,输出-1

思路:

想到用二分图去匹配行列

这题的二分图就比较抽象了,本体实际上的建边方法就是:枚举每一种气球颜色C,将每行中颜色为C的点连给该行,然后枚举所有行进行匈牙利算法即可

为什么呢?这样想:枚举的是行,匹配到的列就是我们要踩的列。若一个行的所有点都已被匹配,就可理解为它们都已经被处理,也就不用去踩这一行了。因此这方法是可行的

#include<bits/stdc++.h>
using namespace std;
const int N=105;
int col[N][N],mk[N],n;
bool mark[N],flag[55];
vector<int>e[N];
bool sp(int x){
    for(int i=0;i<e[x].size();i++){
        int v=e[x][i];
        if(mark[v])continue;
        mark[v]=1;
        if(mk[v]==-1||sp(mk[v])){
            mk[v]=x;
            return true;
        }
    }
    return false;
}
int solve(){
    int ans=0;
    memset(mk,-1,sizeof(mk));
    for(int i=1;i<=n;i++){
        memset(mark,0,sizeof(mark));
        if(sp(i))ans++;
    }
    return ans;
}
int main(){
    int k;
    while(scanf("%d%d",&n,&k)==2){
        if(!n&&!k)break;
        memset(flag,0,sizeof(flag));
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                scanf("%d",&col[i][j]),flag[col[i][j]]=1;
        vector<int>A;A.clear();
        for(int c=1;c<=50;c++){
            if(!flag[c])continue;
            for(int i=1;i<=n;i++)e[i].clear();
            for(int i=1;i<=n;i++){
                for(int j=1;j<=n;j++)
                    if(col[i][j]==c)
                        e[i].push_back(j);
            }
            if(solve()>k)A.push_back(c);
        }
        if(A.size())for(int i=0;i<A.size();i++){printf("%d",A[i]);if(i+1<A.size())putchar(' ');}
        else printf("-1");
        puts("");
    }
    return 0;
}

转载于:https://www.cnblogs.com/Heinz/p/10459045.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值