Codeforces Round #346 (Div. 2) F. Polycarp and Hay(并查集)

F. Polycarp and Hay
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

The farmer Polycarp has a warehouse with hay, which can be represented as an n × m rectangular table, where n is the number of rows, and m is the number of columns in the table. Each cell of the table contains a haystack. The height in meters of the hay located in the i-th row and the j-th column is equal to an integer ai, j and coincides with the number of cubic meters of hay in the haystack, because all cells have the size of the base 1 × 1. Polycarp has decided to tidy up in the warehouse by removing an arbitrary integer amount of cubic meters of hay from the top of each stack. You can take different amounts of hay from different haystacks. Besides, it is allowed not to touch a stack at all, or, on the contrary, to remove it completely. If a stack is completely removed, the corresponding cell becomes empty and no longer contains the stack.

Polycarp wants the following requirements to hold after the reorganization:

  • the total amount of hay remaining in the warehouse must be equal to k,
  • the heights of all stacks (i.e., cells containing a non-zero amount of hay) should be the same,
  • the height of at least one stack must remain the same as it was,
  • for the stability of the remaining structure all the stacks should form one connected region.

The two stacks are considered adjacent if they share a side in the table. The area is called connected if from any of the stack in the area you can get to any other stack in this area, moving only to adjacent stacks. In this case two adjacent stacks necessarily belong to the same area.

Help Polycarp complete this challenging task or inform that it is impossible.

Input

The first line of the input contains three integers nm (1 ≤ n, m ≤ 1000) and k (1 ≤ k ≤ 1018) — the number of rows and columns of the rectangular table where heaps of hay are lain and the required total number cubic meters of hay after the reorganization.

Then n lines follow, each containing m positive integers ai, j (1 ≤ ai, j ≤ 109), where ai, j is equal to the number of cubic meters of hay making the hay stack on the i-th row and j-th column of the table.

Output

In the first line print "YES" (without quotes), if Polycarpus can perform the reorganisation and "NO" (without quotes) otherwise. If the answer is "YES" (without quotes), then in next n lines print m numbers — the heights of the remaining hay stacks. All the remaining non-zero values should be equal, represent a connected area and at least one of these values shouldn't be altered.

If there are multiple answers, print any of them.

Examples
input
2 3 35
10 4 9
9 9 7
output
YES
7 0 7 
7 7 7 
input
4 4 50
5 9 1 1
5 1 1 5
5 1 5 5
5 5 7 1
output
YES
5 5 0 0 
5 0 0 5 
5 0 5 5 
5 5 5 0 
input
2 4 12
1 1 3 1
1 6 2 4
output
NO
Note

In the first sample non-zero values make up a connected area, their values do not exceed the initial heights of hay stacks. All the non-zero values equal 7, and their number is 5, so the total volume of the remaining hay equals the required value k = 7·5 = 35. At that the stack that is on the second line and third row remained unaltered.

题意:给你n, m (1 ≤ n,m ≤ 1000) and k (1 ≤ k ≤ 10^18),n,m表示一个 n*m的矩阵,矩阵中的每个元素小于等于10^9,问能不能构成另外一个矩阵。

这个矩阵里面的数,只能减小,不能增加。

然后你要是的矩阵最后只剩下一个连通块,且连通块里面有一个位置的数没有改变。

连通块的权值和恰好等于k

让你输出一个解。

 

思路:对所有的数从大到小排序,然后把相邻的边也用结构体存起来,边的长度为相邻两点的较小值,从大到小排序,然后用并查集维护,如果如果当前的边的长度大于等于当前的长度,则把两个点并起来,维护这个集合中的结点的数目。只要当前这个连通块的大小大于等于k/a[i][j]就好了

然后输出的时候用bfs去输出,去维护这个连通块的大小。

坑点:有可能最后的矩阵只剩下一个数。

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PI;
typedef __int64 ll;
const int maxn=4001000;
int f[maxn/4],sz[maxn/4];

int find(int x)
{
    int k, j, r;
    r = x;
    while(r != f[r])
        r = f[r];
    k = x;
    while(k != r){
        j = f[k];
        f[k] = r;
        k = j;
    }
    return r;
}

struct Edge{
    int from,to,num;
}e[maxn];

int a[1010][1010],num[maxn];
int vis[1010][1010];
int dir[4][2]={1,0,0,1,-1,0,0,-1};

bool cmp(int x,int y){
    return x>y;
}

bool cmp1(Edge x,Edge y){
    return x.num>y.num;
}

struct node{
    int x,y;
};

map<int,int>mp;
int T,n,m;
ll ans1;

bool bfs(int ans,int count,int x,int y){
    queue<node>Q;
    Q.push((node){x,y});
    vis[x][y]=T;
    int cnt=1;
    while(!Q.empty()){
        node u=Q.front();
        Q.pop();
        if(cnt==ans)
            return true;
        for(int i=0;i<4;i++){
            int x=u.x+dir[i][0],y=u.y+dir[i][1];
            if(x<=0||x>n||y<=0||y>m||a[x][y]<count||vis[x][y]!=0)
                continue;
            vis[x][y]=T;
            Q.push((node){x,y});
            cnt++;
            if(cnt==ans)
                return true;
        }
    }
    return false;
}

void solve(int cnt,int count){
    int flag=0;
    T=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            T++;
            if(a[i][j]==count&&vis[i][j]==0)
                if(bfs(cnt,count,i,j)){
                    flag=1;
                    break;
                }
        }
        if(flag==1)
            break;
    }
}

int main(){
    int tot=0;
    ll k;
    scanf("%d%d%I64d",&n,&m,&k);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            scanf("%d",&a[i][j]);
            num[++tot]=a[i][j];
        }
    sort(num+1,num+tot+1,cmp);
    tot=unique(num+1,num+tot+1)-num-1;
    int cnt=0;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            for(int u=0;u<2;u++){
                int x=i+dir[u][0],y=j+dir[u][1];
                if(x<=0||x>n||y<=0||y>m)
                    continue;
                e[++cnt].from=(i-1)*m+j,e[cnt].to=(x-1)*m+y;
                e[cnt].num=min(a[i][j],a[x][y]);
            }
    for(int i=1;i<=n*m;i++)
        f[i]=i,sz[i]=1;
    sort(e+1,e+cnt+1,cmp1);
    for(int i=1,j=1;i<=tot;i++){
        int maxv=1; //最少有一个
        while(j<=cnt&&e[j].num==num[i]){
            int x=find(e[j].from),y=find(e[j].to);
            if(x!=y){
                f[x]=y;
                sz[y]+=sz[x];
            }
            j++;
            maxv=max(maxv,sz[y]);
        }
        mp[num[i]]=maxv;
    }
    ll flag=0,ans;
    for(int i=1;i<=tot;i++){
        if(k%num[i]==0&&mp[num[i]]>=k/num[i]){
            flag=1;
            ans=num[i];
            solve(k/num[i],num[i]);
            break;
        }
    }
    if(flag==0)
        printf("NO\n");
    else{
        printf("YES\n");
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++){
                if(j==m)
                    printf("%d\n",vis[i][j]==T ? ans:0);
                else
                    printf("%d ",vis[i][j]==T ? ans:0);
            }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值