50 years, 50 colors HDU - 1498(二分图最大匹配)

这篇博客介绍了如何运用二分图的最小点覆盖数(二分图的最大匹配)来解决一个趣味数学问题:在一个n*n的矩阵中,有50种颜色的气球,每次可以选择一行或一列扎破相同颜色的气球,求在有限次操作后哪些颜色无法被全部扎破。博主通过代码实现了一个匈牙利算法来解决这个问题,并给出了具体的案例分析和输出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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的矩阵,每个位置上都有一个气球,气球共有50种颜色,编号为1~50,给你k次机会,扎气球,有两种方式,第一种是选择一行扎破相同颜色相同的气球,第二种是选择一列相同颜色相同的气球,问在k次操作之后,有哪几种气球不能被完全扎破,如果有,按从小到大的顺序输出,没有则输出-1

思路

二分图的最小点覆盖数(二分图的最大匹配)

行为左边,列为右边,对每种颜色判断这种颜色的气球最少需要多少次才能完全被扎破,尽可能的选出小的行号或列号,然后判断是否正好把所有颜色的气球都扎破了

代码

#include<bits/stdc++.h>
using namespace std;
int n,k;
int a[110][110];
int col[60];
int book[110];
int match[110];
int newcol;
int dfs(int u)
{
    for(int i=1;i<=n;i++)
    {
        if(book[i]==0&&a[u][i]==newcol)
        {
            book[i]=1;
            if(match[i]==0||dfs(match[i]))
            {
                match[i]=u;
                return 1;
            }
        }
    }
    return 0;
}
int hungarian(int u)
{
    newcol=u;//记录当前颜色
    memset(match,0,sizeof(match));
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        memset(book,0,sizeof(book));
        if(dfs(i))
            ans++;
    }
    return ans;
}
int main()
{
    while(~scanf("%d %d",&n,&k)&&n+k)
    {
        memset(a,0,sizeof(a));
        memset(col,0,sizeof(col));
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                scanf("%d",&a[i][j]);
                col[a[i][j]]++;//记录每个颜色出现的次数
            }
        }
        vector<int>v;
        for(int i=1;i<=50;i++)
        {
            if(col[i]>0)
            {
                if(hungarian(i)>k)
                {
                    v.push_back(i);//插入i
                }
            }
        }
        if(v.size()==0)
            printf("-1\n");
        else
        {
            printf("%d",v[0]);
            for(int i=1;i<v.size();i++)
                printf(" %d",v[i]);
            printf("\n");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值