HDU - 1498 G.题解

Posted studyshare777

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU - 1498 G.题解相关的知识,希望对你有一定的参考价值。

G - 同上

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*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的方格中,有不同颜色的气球,选定一种颜色,每次可以选择一行或一列,使你选择的行或列所在的你选定颜色的气球爆掉。对于每种颜色,问操作k次后,气球不能全部爆掉的气球有哪些。不存在这种颜色就输出-1。

分析:

即然每次选一行或一列,自然想到每次选一个横坐标或纵坐标。先讨论一种颜色,对方格中有该颜色的位置(x,y),x-y建边,把选择的横坐标或纵坐标看成点,要该位置的气球爆掉,就是要选一个点,能覆盖x-y边,这样就是最小点覆盖问题了。

然后分别对每种颜色都操作一边,判断是否大于k,大于k输出。

代码:

 

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<deque>
#include<cmath>
#include<map>
#define sd(x) scanf("%d",&x)
#define ms(x) memset(x,0,sizeof x)
#define fr(i,n) for(int i=1;i<=n;i++)
using namespace std;
typedef long long ll;
const int maxn=106;
const int N=1e5+6;
const int INF=0x4f4f4f4f;
int mat[maxn]; 
bool vis[maxn];
vector<int>to[maxn];
struct node
{
    int x,y;
    int col;
}a[N];
bool find(int x)
{
    vis[x]=1;
    for(int i=0;i<to[x].size();i++)
    {
        int y=to[x][i];
        if(!mat[y])
        {
            mat[y]=x;
            return 1;
        }
        else 
        {
            int nxt=mat[y];
            if(!vis[nxt])
            {
                if(find(nxt)) 
                {
                    mat[y]=x;
                    return 1;
                }
            }
        }
    }
    return 0;
}
bool cmp(struct node a,struct node b)
{
    return a.col<b.col;
}
int n,m,k,x,y,cnt;
void add()//对每个颜色建边
{
    for(int i=1;i<=n;i++) to[i].clear();//清空上个颜色的边 
    int color=a[cnt].col;
    while(cnt<m&&a[cnt].col==color)//找到同一颜色的边 
    {
        x=a[cnt].x,y=a[cnt].y;
        to[y].push_back(x);
        cnt++;
    }
} 
int solve()
{
    int ans=0;
    ms(mat);
    for(int i=1;i<=n;i++)
    {
        ms(vis);
        if(find(i)) ans++;
        if(ans>k) break;
    }
    return ans;
}
int main()
{
    while(1)
    {
        scanf("%d%d",&n,&k);
        if(n==0&&k==0) break;
        cnt=0,m=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                int c;
                sd(c);
                a[m].y=i,a[m].x=j;
                a[m++].col=c;
            }
        }
        sort(a,a+m,cmp);
        vector<int> pr_ans;
        pr_ans.clear();
        while(cnt<m)
        {
            add();
            int res=solve();
            if(res>k)
            {
                pr_ans.push_back(a[cnt-1].col);
            }
        }
        if(pr_ans.empty()) printf("-1
");
        else
        {
            for(int i=0;i<pr_ans.size();i++)
            printf("%d%c",pr_ans[i],i==pr_ans.size()-1?
: );
        }
    }
    return 0;
}

 

以上是关于HDU - 1498 G.题解的主要内容,如果未能解决你的问题,请参考以下文章

ACM-ICPC 2017 沈阳赛区现场赛 G. Infinite Fraction Path && HDU 6223

HDU——T 1498 50 years, 50 colors

HDU——1498 50 years, 50 colors

hdu 1498 50 years, 50 colors 最小点覆盖

HDU4089/Uva1498 Activation 概率DP(好题)

G. The Galactic Olympics(2016-2017 ACM-ICPC, Egyptian Collegiate Programming Contest (ECPC 16)题解)(代码