codeforces 659F (并查集 bfs)

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.


题意:

构造一个矩阵,使得:

矩阵所有格子中数字都小于等于原矩阵,并且至少有一个元素和原矩阵相等,构造的矩阵除了0以外的数字

必须联通并且相等.

对每个格子的数字进行排序,那么一个格子的数字最多能够填的格子数就是他上下左右格子能够填的格子

数的和,这个可以用并查集来维护.

然后枚举每个格子,如果这个格子的数字能够整除k并且这个格子能够填的个数足够,就可以从这个格子出发

bfs一遍找到需要的格子.

#include <bits/stdc++.h>
using namespace std;
#define maxn 1005

int n, m;
long long k;
long long mp[maxn][maxn];
long long tot[maxn*maxn];
struct node {
    int x, y;
    long long num;
    bool operator < (const node &a) const {
        return num > a.num;
    }
}p[maxn*maxn];
int cnt;

int fa[maxn*maxn];
#define find Find
int find (int x) {
    return fa[x] == x ? fa[x] : fa[x] = find (fa[x]);
}
#define move Move
const int move[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

bool legal (int x, int y) {
    if(x < 0 || y < 0 || x >= n || y >= m)
        return 0;
    return 1;
}
int id (int x, int y) {
    return x*m+y;
}
bool vis[maxn][maxn];

void bfs (int x, int y, int cnt) { 
   	int tou = 0, wei = 0;
    p[wei++] = (node) {x, y, 0};
    memset (vis, 0, sizeof vis);
    vis[x][y] = 1;
    cnt--;
    while (tou < wei && cnt) {
        node now = p[tou++];
        vis[now.x][now.y] = 1;
        for (int i = 0; i < 4; i++) {
            int xx = now.x+move[i][0], yy = now.y+move[i][1];
            if (!legal (xx, yy) || vis[xx][yy]) {
                continue;
            }
            if (mp[xx][yy] >= mp[x][y]) {
                vis[xx][yy] = 1;
                p[wei++] = (node) {xx, yy, 0};
                cnt--;
                if (cnt <= 0) {
                    break;
                }
            }
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (vis[i][j])
                printf ("%lld", mp[x][y]);
            else
                printf ("0");
            printf ("%c", j == m-1 ? '\n' : ' ');
        }
    }
}

int main () {
    //freopen ("in.txt", "r", stdin);
    scanf ("%d%d%lld", &n, &m, &k);
    cnt = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            scanf ("%lld", &mp[i][j]);
            p[cnt++] = (node) {i, j, mp[i][j]};
            tot[id (i, j)] = 1;
        }
    }
    for (int i = 0; i <= n*m; i++) {
        fa[i] = i;
    }
    memset (vis, 0, sizeof vis);
    sort (p, p+cnt);
    for (int i = 0; i < cnt; i++) {
        int x = p[i].x, y = p[i].y;
        vis[x][y] = 1;
        for (int j = 0; j < 4; j++) {
            int xx = x+move[j][0], yy = y+move[j][1];
            if (!legal (xx, yy) || !vis[xx][yy])
                continue;
            int p1 = find (id (x, y)), p2 = find (id (xx, yy));
            if (p1 == p2)
                continue;
            fa[p2] = p1;
            tot[p1] += tot[p2];
        }
    } 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (mp[i][j] && k%mp[i][j] == 0 && tot[id (i, j)] >= k/mp[i][j]) { 
                printf ("YES\n");
                bfs (i, j, k/mp[i][j]);
                goto out;
            }
        }
    }
    printf ("NO\n");
    out: ;
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值